Search in sources :

Example 41 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class RequestValidatorTest method testValidatePasswordAndEmailDisabledHeaderCheck.

/* Disable header check */
@Test
void testValidatePasswordAndEmailDisabledHeaderCheck() {
    var propertyValidator = mock(PropertyValidator.class);
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, false);
    when(propertyValidator.isValidPropertiesMap(anyMap())).thenReturn(true);
    Email email = new Email("test@test.com", false, "token");
    User user = new User(email, "password");
    assertDoesNotThrow(() -> validator.validate(null, "test@test.com", false));
    assertDoesNotThrow(() -> validator.validate("", "test@test.com", false));
    assertDoesNotThrow(() -> validator.validate(null, "test@test.com", user));
    assertDoesNotThrow(() -> validator.validate("", "test@test.com", user));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Example 42 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testSuccessfulEmailUpdate.

@Test
void testSuccessfulEmailUpdate() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterableNew = mock(FindIterable.class);
    when(findIterableNew.first()).thenReturn(null);
    doReturn(findIterableNew).when(collection).find(eq(Filters.eq("_id", USER.getEmail().getAddress())));
    FindIterable<Document> findIterableExisting = mock(FindIterable.class);
    when(findIterableExisting.first()).thenReturn(DOCUMENT);
    doReturn(findIterableExisting).when(collection).find(eq(Filters.eq("_id", "existingEmail")));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    User result = usersDao.update("existingEmail", USER).join();
    // The creation time and update time will have been reset since an email update
    // triggers a delete and insert
    long creationTime = (Long) result.getProperties().get("creationTime");
    long updateTime = (Long) result.getProperties().get("lastUpdateTime");
    assertAll("The creation and update time are correct", () -> assertTrue(creationTime > CURR_TIME), () -> assertTrue(updateTime > CURR_TIME), () -> assertEquals(creationTime, updateTime));
    assertEquals(USER.withTime(creationTime, updateTime), result);
    verify(collection, times(1)).find(eq(Filters.eq("_id", "existingEmail")));
    verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
    verify(collection, times(1)).insertOne(argThat((Document doc) -> doc.containsKey("_id") && doc.containsKey("id") && doc.containsKey("version") && doc.containsKey("creation_time") && doc.containsKey("update_time") && doc.containsKey("document")));
    verify(collection, times(1)).deleteOne(eq(Filters.eq("_id", "existingEmail")));
}
Also used : User(com.sanctionco.thunder.models.User) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 43 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testSuccessfulUpdate.

@Test
void testSuccessfulUpdate() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    when(findIterable.first()).thenReturn(DOCUMENT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    User result = usersDao.update(null, USER).join();
    // Update will set the update time
    long creationTime = (Long) result.getProperties().get("creationTime");
    long updateTime = (Long) result.getProperties().get("lastUpdateTime");
    assertAll("The creation and update time are correct", () -> assertEquals(CURR_TIME, creationTime), () -> assertTrue(updateTime > CURR_TIME));
    assertEquals(USER.withTime(creationTime, updateTime), result);
    verify(collection, times(1)).find(any(Bson.class));
    verify(collection, times(1)).updateOne(argThat((Bson bson) -> {
        BsonDocument doc = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
        return doc.containsKey("version");
    }), any(Bson.class));
}
Also used : User(com.sanctionco.thunder.models.User) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 44 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testSuccessfulDelete.

@Test
void testSuccessfulDelete() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    when(findIterable.first()).thenReturn(DOCUMENT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    User result = usersDao.delete("test@test.com").join();
    assertEquals(USER.withTime(CURR_TIME, CURR_TIME), result);
    verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
    verify(collection, times(1)).deleteOne(eq(Filters.eq("_id", "test@test.com")));
}
Also used : User(com.sanctionco.thunder.models.User) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 45 with User

use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.

the class ThunderClientTest method testSendVerificationEmail.

@Test
void testSendVerificationEmail() throws Exception {
    User response = client.sendVerificationEmail("email", password).get();
    assertEquals(user.getEmail(), response.getEmail());
}
Also used : User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Aggregations

User (com.sanctionco.thunder.models.User)51 Test (org.junit.jupiter.api.Test)41 Email (com.sanctionco.thunder.models.Email)26 RequestValidator (com.sanctionco.thunder.validation.RequestValidator)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 UsersDao (com.sanctionco.thunder.dao.UsersDao)8 Objects (java.util.Objects)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 UUID (java.util.UUID)7 BsonDocument (org.bson.BsonDocument)6 Document (org.bson.Document)6 Metered (com.codahale.metrics.annotation.Metered)5 Counter (com.codahale.metrics.Counter)4 MetricRegistry (com.codahale.metrics.MetricRegistry)4 ThunderException (com.sanctionco.thunder.ThunderException)4 SwaggerAnnotations (com.sanctionco.thunder.openapi.SwaggerAnnotations)4 MetricNameUtil (com.sanctionco.thunder.util.MetricNameUtil)4 RequestValidationException (com.sanctionco.thunder.validation.RequestValidationException)4 POST (javax.ws.rs.POST)4