Skip to content

Commit

Permalink
contract test setup for mfa storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Oct 25, 2024
1 parent 6c7f6cd commit 1f6dd96
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.vauthenticator.server.mfa.adapter

import com.vauthenticator.server.keys.domain.*
import com.vauthenticator.server.mfa.domain.MfaAccountMethod
import com.vauthenticator.server.mfa.domain.MfaAccountMethodsRepository
import com.vauthenticator.server.mfa.domain.MfaDeviceId
import com.vauthenticator.server.mfa.domain.MfaMethod
import com.vauthenticator.server.support.AccountTestFixture.anAccount
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.*

abstract class AbstractMfaAccountMethodsRepositoryTest {

val mfaDeviceId = MfaDeviceId("A_MFA_DEVICE_ID")
val masterKid = MasterKid("")
val email = anAccount().email
val key = Kid("")

@MockK
lateinit var keyRepository: KeyRepository

lateinit var uut: MfaAccountMethodsRepository
abstract fun initMfaAccountMethodsRepository(): MfaAccountMethodsRepository
abstract fun resetDatabase()
@BeforeEach
fun setUp() {

resetDatabase()
uut = initMfaAccountMethodsRepository()
}

@Test
fun `when a mfa account method is stored`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns key

uut.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = uut.findAll(email)
assertEquals(
listOf(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)
}

@Test
fun `when a mfa account do not have method stored`() {
val mfaAccountMethods = uut.findAll(email)
assertEquals(emptyList<MfaAccountMethod>(), mfaAccountMethods)
}

@Test
fun `when try to get one specific enrolment association`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns Kid("")

uut.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = uut.findBy(email, MfaMethod.EMAIL_MFA_METHOD, email)
assertEquals(
Optional.of(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)
}

@Test
fun `when one specific mfa account method is found by device id`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns Kid("")

val savedMfaAccountMethod = uut.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = uut.findBy(savedMfaAccountMethod.mfaDeviceId)

assertEquals(
Optional.of(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)

}

@Test
fun `when one specific enrolment association is not found`() {
val mfaAccountMethods = uut.findBy(email, MfaMethod.EMAIL_MFA_METHOD, email)
val expected = Optional.empty<Any>()
assertEquals(expected, mfaAccountMethods)
}

@Test
fun `when decide what mfa use as default`() {
val expected = Optional.of(mfaDeviceId)
uut.setAsDefault(email, mfaDeviceId)
val defaultDevice = uut.getDefaultDevice(email)

assertEquals(expected, defaultDevice)
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
package com.vauthenticator.server.mfa.adapter.dynamodb

import com.vauthenticator.server.keys.domain.*
import com.vauthenticator.server.mfa.domain.MfaAccountMethod
import com.vauthenticator.server.mfa.adapter.AbstractMfaAccountMethodsRepositoryTest
import com.vauthenticator.server.mfa.domain.MfaAccountMethodsRepository
import com.vauthenticator.server.mfa.domain.MfaDeviceId
import com.vauthenticator.server.mfa.domain.MfaMethod
import com.vauthenticator.server.support.AccountTestFixture.anAccount
import com.vauthenticator.server.support.DynamoDbUtils.dynamoDbClient
import com.vauthenticator.server.support.DynamoDbUtils.dynamoDefaultMfaAccountMethodsTableName
import com.vauthenticator.server.support.DynamoDbUtils.dynamoMfaAccountMethodsTableName
import com.vauthenticator.server.support.DynamoDbUtils.resetDynamoDb
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.util.*

@ExtendWith(MockKExtension::class)
class DynamoMfaAccountMethodsRepositoryTest {
class DynamoMfaAccountMethodsRepositoryTest : AbstractMfaAccountMethodsRepositoryTest(){

private val mfaDeviceId = MfaDeviceId("A_MFA_DEVICE_ID")
private val masterKid = MasterKid("")
private val email = anAccount().email
private val key = Kid("")

@MockK
lateinit var keyRepository: KeyRepository

lateinit var underTest: MfaAccountMethodsRepository

@BeforeEach
fun setUp() {

resetDynamoDb()
underTest = DynamoMfaAccountMethodsRepository(
override fun initMfaAccountMethodsRepository(): MfaAccountMethodsRepository {
return DynamoMfaAccountMethodsRepository(
dynamoMfaAccountMethodsTableName,
dynamoDefaultMfaAccountMethodsTableName,
dynamoDbClient,
Expand All @@ -45,63 +22,7 @@ class DynamoMfaAccountMethodsRepositoryTest {
) { mfaDeviceId }
}

@Test
fun `when a mfa account method is stored`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns key

underTest.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = underTest.findAll(email)
assertEquals(
listOf(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)
}

@Test
fun `when a mfa account do not have method stored`() {
val mfaAccountMethods = underTest.findAll(email)
assertEquals(emptyList<MfaAccountMethod>(), mfaAccountMethods)
}

@Test
fun `when try to get one specific enrolment association`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns Kid("")

underTest.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = underTest.findBy(email, MfaMethod.EMAIL_MFA_METHOD, email)
assertEquals(
Optional.of(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)
}

@Test
fun `when one specific mfa account method is found by device id`() {
every { keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) } returns Kid("")

val savedMfaAccountMethod = underTest.save(email, MfaMethod.EMAIL_MFA_METHOD, email, true)
val mfaAccountMethods = underTest.findBy(savedMfaAccountMethod.mfaDeviceId)

assertEquals(
Optional.of(MfaAccountMethod(email, mfaDeviceId, key, MfaMethod.EMAIL_MFA_METHOD, email, true)),
mfaAccountMethods
)

}

@Test
fun `when one specific enrolment association is not found`() {
val mfaAccountMethods = underTest.findBy(email, MfaMethod.EMAIL_MFA_METHOD, email)
val expected = Optional.empty<Any>()
assertEquals(expected, mfaAccountMethods)
}

@Test
fun `when decide what mfa use as default`() {
val expected = Optional.of(mfaDeviceId)
underTest.setAsDefault(email, mfaDeviceId)
val defaultDevice = underTest.getDefaultDevice(email)

assertEquals(expected, defaultDevice)
override fun resetDatabase() {
resetDynamoDb()
}
}

0 comments on commit 1f6dd96

Please sign in to comment.