Author: matthieu
Date: Fri Dec 11 12:31:43 2015
New Revision: 1719375

URL: http://svn.apache.org/viewvc?rev=1719375&view=rev
Log:
JAMES-1644 Use in memory UsersRepository when testing

Added:
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserCredentials.java
Modified:
    james/project/trunk/server/protocols/jmap/pom.xml
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java

Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719375&r1=1719374&r2=1719375&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 12:31:43 2015
@@ -171,6 +171,17 @@
                 </dependency>
                 <dependency>
                     <groupId>org.apache.james</groupId>
+                    <artifactId>james-server-data-library</artifactId>
+                    <scope>test</scope>
+                </dependency>
+                <dependency>
+                    <groupId>org.apache.james</groupId>
+                    <artifactId>james-server-data-library</artifactId>
+                    <scope>test</scope>
+                    <type>test-jar</type>
+                </dependency>
+                <dependency>
+                    <groupId>org.apache.james</groupId>
                     <artifactId>james-server-jetty</artifactId>
                 </dependency>
                 <dependency>

Modified: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java?rev=1719375&r1=1719374&r2=1719375&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
 Fri Dec 11 12:31:43 2015
@@ -47,7 +47,7 @@ import org.apache.james.jmap.model.Conti
 import org.apache.james.jmap.utils.ZonedDateTimeProvider;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.user.api.UsersRepository;
-import org.apache.james.user.api.UsersRepositoryException;
+import org.apache.james.user.lib.mock.InMemoryUsersRepository;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -64,20 +64,21 @@ public class JMAPAuthenticationTest {
 
     private JettyHttpServer server;
 
-    private UsersRepository mockedUsersRepository;
+    private UsersRepository usersRepository;
     private ZonedDateTimeProvider mockedZonedDateTimeProvider;
     private AccessTokenManager accessTokenManager;
     private SignedContinuationTokenManager continuationTokenManager;
+    private UserCredentials userCredentials;
 
     @Before
     public void setup() throws Exception {
-        mockedUsersRepository = mock(UsersRepository.class);
+        usersRepository = new InMemoryUsersRepository();
         mockedZonedDateTimeProvider = mock(ZonedDateTimeProvider.class);
         MailboxManager mockedMailboxManager = mock(MailboxManager.class);
         accessTokenManager = new AccessTokenManagerImpl(new 
MemoryAccessTokenRepository(TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)));
         continuationTokenManager = new SignedContinuationTokenManager(new 
JamesSignatureHandlerProvider().provide(), mockedZonedDateTimeProvider);
         
-        AuthenticationServlet authenticationServlet = new 
AuthenticationServlet(mockedUsersRepository, continuationTokenManager, 
accessTokenManager);
+        AuthenticationServlet authenticationServlet = new 
AuthenticationServlet(usersRepository, continuationTokenManager, 
accessTokenManager);
 
         AuthenticationFilter authenticationFilter = new 
AuthenticationFilter(accessTokenManager, mockedMailboxManager);
         Filter getAuthenticationFilter = new 
BypassOnPostFilter(authenticationFilter);
@@ -95,13 +96,19 @@ public class JMAPAuthenticationTest {
         server.start();
         RestAssured.port = server.getPort();
         RestAssured.config = 
newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8));
+
+        userCredentials = UserCredentials.builder()
+            .username("user")
+            .password("password")
+            .build();
+        usersRepository.addUser(userCredentials.getUsername(), 
userCredentials.getPassword());
     }
-    
+
     @After
     public void teardown() throws Exception {
         server.stop();
     }
-    
+
     @Test
     public void mustReturnMalformedRequestWhenContentTypeIsMissing() {
         given()
@@ -186,7 +193,7 @@ public class JMAPAuthenticationTest {
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"username\": \"u...@domain.tld\", \"clientName\": 
\"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", \"deviceName\": \"Joe 
Blogg’s iPhone\"}")
+            .body("{\"username\": \"" + userCredentials.getUsername() + "\", 
\"clientName\": \"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", 
\"deviceName\": \"Joe Blogg’s iPhone\"}")
         .when()
             .post("/authentication")
         .then()
@@ -202,12 +209,12 @@ public class JMAPAuthenticationTest {
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"username\": \"u...@domain.tld\", \"clientName\": 
\"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", \"deviceName\": \"Joe 
Blogg’s iPhone\"}")
+            .body("{\"username\": \"" + userCredentials.getUsername() + "\", 
\"clientName\": \"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", 
\"deviceName\": \"Joe Blogg’s iPhone\"}")
         .when()
             .post("/authentication")
         .then()
             .statusCode(200)
-            .body("methods", hasItem("password"));
+            .body("methods", hasItem(userCredentials.getPassword()));
     }
 
     @Test
@@ -218,7 +225,7 @@ public class JMAPAuthenticationTest {
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"username\": \"u...@domain.tld\", \"clientName\": 
\"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", \"deviceName\": \"Joe 
Blogg’s iPhone\"}")
+            .body("{\"username\": \"" + userCredentials.getUsername() + "\", 
\"clientName\": \"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", 
\"deviceName\": \"Joe Blogg’s iPhone\"}")
         .when()
             .post("/authentication")
         .then()
@@ -245,17 +252,15 @@ public class JMAPAuthenticationTest {
 
     @Test
     public void 
mustReturnAuthenticationFailedWhenContinuationTokenIsRejectedByTheContinuationTokenManager()
 throws Exception {
-        ContinuationToken badContinuationToken = new 
ContinuationToken("u...@domain.tld", newDate, "badSignature");
+        ContinuationToken badContinuationToken = new 
ContinuationToken(userCredentials.getUsername(), newDate, "badSignature");
         
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(oldDate);
 
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + badContinuationToken.serialize() + "\", 
\"method\": \"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + badContinuationToken.serialize() + "\", 
\"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + 
"\"}")
         .when()
             .post("/authentication")
         .then()
@@ -269,15 +274,13 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
         
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(afterExpirationDate);
 
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"" + userCredentials.getPassword() + "\"}")
         .when()
             .post("/authentication")
         .then()
@@ -291,13 +294,12 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
 
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenThrow(new UsersRepositoryException("test"));
+        usersRepository.removeUser(userCredentials.getUsername());
 
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"" + userCredentials.getPassword() + "\"}")
         .when()
             .post("/authentication")
         .then()
@@ -311,15 +313,13 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
 
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(newDate);
 
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"" + userCredentials.getPassword() + "\"}")
         .when()
             .post("/authentication")
         .then()
@@ -333,15 +333,13 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
 
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(newDate);
 
         given()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"" + userCredentials.getPassword() + "\"}")
         .when()
             .post("/authentication")
         .then()
@@ -386,8 +384,6 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
     
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(newDate);
     
@@ -448,8 +444,6 @@ public class JMAPAuthenticationTest {
 
         String continuationToken = fromGoodContinuationTokenRequest();
     
-        when(mockedUsersRepository.test("u...@domain.tld", "password"))
-            .thenReturn(true);
         when(mockedZonedDateTimeProvider.get())
             .thenReturn(newDate);
     
@@ -469,7 +463,7 @@ public class JMAPAuthenticationTest {
         return with()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"username\": \"u...@domain.tld\", \"clientName\": 
\"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", \"deviceName\": \"Joe 
Blogg’s iPhone\"}")
+            .body("{\"username\": \"" + userCredentials.getUsername() + "\", 
\"clientName\": \"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", 
\"deviceName\": \"Joe Blogg’s iPhone\"}")
         .post("/authentication")
             .body()
             .path("continuationToken")
@@ -480,7 +474,7 @@ public class JMAPAuthenticationTest {
         return with()
             .contentType(ContentType.JSON)
             .accept(ContentType.JSON)
-            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"password\"}")
+            .body("{\"token\": \"" + continuationToken + "\", \"method\": 
\"password\", \"password\": \"" + userCredentials.getPassword() + "\"}")
         .post("/authentication")
             .path("accessToken")
             .toString();

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserCredentials.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserCredentials.java?rev=1719375&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserCredentials.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserCredentials.java
 Fri Dec 11 12:31:43 2015
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap;
+
+public class UserCredentials {
+
+    public static Builder builder() {
+        return new Builder();
+    }
+    
+    public static class Builder {
+        
+        private String username;
+        private String password;
+        
+        private Builder() {
+        }
+        
+        public Builder username(String username) {
+            this.username = username;
+            return this;
+        }
+        
+        public Builder password(String password) {
+            this.password = password;
+            return this;
+        }
+        
+        public UserCredentials build() {
+            return new UserCredentials(username, password);
+        }
+    }
+
+    private final String username;
+    private final String password;
+
+    private UserCredentials(String username, String password) {
+        this.username = username;
+        this.password = password;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to