-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor dynamodb test into contract test
- Loading branch information
Showing
4 changed files
with
155 additions
and
61 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...n/kotlin/com/vauthenticator/server/password/adapter/jdbc/JdbcPasswordHistoryRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.vauthenticator.server.password.adapter.jdbc | ||
|
||
import com.vauthenticator.server.password.domain.Password | ||
import com.vauthenticator.server.password.domain.PasswordHistoryRepository | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue | ||
import java.time.Clock | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
|
||
class JdbcPasswordHistoryRepository( | ||
private val historyEvaluationLimit: Int, | ||
private val maxHistoryAllowedSize: Int, | ||
private val clock: Clock, | ||
private val dynamoPasswordHistoryTableName: String, | ||
private val jdbcTemplate: JdbcTemplate | ||
) : PasswordHistoryRepository { | ||
override fun store(userName: String, password: Password) { | ||
// dynamoDbClient.putItem( | ||
// PutItemRequest.builder() | ||
// .tableName(dynamoPasswordHistoryTableName) | ||
// .item( | ||
// mapOf( | ||
// "user_name" to userName.asDynamoAttribute(), | ||
// "created_at" to createdAt().asDynamoAttribute(), | ||
// "password" to password.content.asDynamoAttribute() | ||
// ) | ||
// ) | ||
// .build() | ||
// ) | ||
} | ||
|
||
private fun createdAt() = | ||
LocalDateTime.now(clock) | ||
.toInstant(ZoneOffset.UTC) | ||
.toEpochMilli() | ||
|
||
override fun load(userName: String): List<Password> { | ||
/* val items = dynamoDbClient.query( | ||
QueryRequest.builder() | ||
.tableName(dynamoPasswordHistoryTableName) | ||
.scanIndexForward(false) | ||
.keyConditionExpression("user_name=:email") | ||
.expressionAttributeValues(mapOf(":email" to userName.asDynamoAttribute())).build() | ||
).items() | ||
val allowedPassword = items.take(historyEvaluationLimit) | ||
deleteUselessPasswordHistory(items) | ||
return allowedPassword.map { Password(it.valueAsStringFor("password")) }*/ | ||
TODO() | ||
} | ||
|
||
private fun deleteUselessPasswordHistory(itemsInTheHistory: List<Map<String, AttributeValue>>) { | ||
val leftoverSize = itemsInTheHistory.size - maxHistoryAllowedSize | ||
if (leftoverSize > 0) { | ||
// itemsInTheHistory.takeLast(leftoverSize) | ||
// .forEach { itemToDelete -> | ||
// dynamoDbClient.deleteItem( | ||
// DeleteItemRequest.builder() | ||
// .tableName(dynamoPasswordHistoryTableName) | ||
// .key(itemToDelete.filterKeys { it != "password" }) | ||
// .build() | ||
// ) | ||
// } | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...otlin/com/vauthenticator/server/password/adapter/AbstractPasswordHistoryRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.vauthenticator.server.password.adapter | ||
|
||
import com.vauthenticator.server.password.domain.Password | ||
import com.vauthenticator.server.password.domain.PasswordHistoryRepository | ||
import io.mockk.junit5.MockKExtension | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
private const val A_USERNAME = "A_USERNAME" | ||
|
||
abstract class AbstractPasswordHistoryRepositoryTest { | ||
|
||
lateinit var uut: PasswordHistoryRepository | ||
|
||
abstract fun initPasswordHistoryRepository(): PasswordHistoryRepository | ||
abstract fun resetDatabase() | ||
abstract fun loadActualDynamoSizeFor(userName: String): List<Map<String, Any>> | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
uut = initPasswordHistoryRepository() | ||
resetDatabase() | ||
} | ||
|
||
@Test | ||
fun `when store a new password in an empty the history`() { | ||
|
||
uut.store(A_USERNAME, Password("A_PASSWORD")) | ||
val history = uut.load(A_USERNAME) | ||
|
||
Assertions.assertEquals(1, history.size) | ||
} | ||
|
||
@Test | ||
fun `when store a new password in an non empty the history`() { | ||
uut.store(A_USERNAME, Password("A_PASSWORD 3")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 1")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 2")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 10")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 12")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 22")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 32")) | ||
uut.store(A_USERNAME, Password("A_PASSWORD 42")) | ||
val history = uut.load(A_USERNAME) | ||
|
||
val expected = listOf( | ||
Password("A_PASSWORD 42"), | ||
Password("A_PASSWORD 32"), | ||
) | ||
Assertions.assertEquals(expected, history) | ||
Assertions.assertEquals(3, loadActualDynamoSizeFor(A_USERNAME).size) | ||
} | ||
|
||
@Test | ||
fun `when store a new password in an non empty the history with less item then requested as limit`() { | ||
uut.store(A_USERNAME, Password("A_PASSWORD 3")) | ||
val history = uut.load(A_USERNAME) | ||
|
||
val expected = listOf( | ||
Password("A_PASSWORD 3"), | ||
) | ||
Assertions.assertEquals(expected, history) | ||
Assertions.assertEquals(1, loadActualDynamoSizeFor(A_USERNAME).size) | ||
} | ||
|
||
} |
76 changes: 15 additions & 61 deletions
76
...om/vauthenticator/server/password/adapter/dynamodb/DynamoPasswordHistoryRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...tlin/com/vauthenticator/server/password/adapter/jdbc/JdbcPasswordHistoryRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.vauthenticator.server.password.adapter.jdbc | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
|
||
class JdbcPasswordHistoryRepositoryTest |