-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPasskeyChallengeDBTest.scala
More file actions
141 lines (128 loc) · 4.07 KB
/
PasskeyChallengeDBTest.scala
File metadata and controls
141 lines (128 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package aws
import aws.PasskeyChallengeDB.*
import com.gu.googleauth.UserIdentity
import com.webauthn4j.data.client.challenge.DefaultChallenge
import models.PasskeyFlow.{Authentication, Registration}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
import software.amazon.awssdk.services.dynamodb.model.KeyType.{HASH, RANGE}
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType.S
import software.amazon.awssdk.services.dynamodb.model.*
import java.nio.charset.StandardCharsets.UTF_8
import java.time.ZoneOffset.UTC
import java.time.{Clock, Instant}
import scala.util.{Failure, Success}
/** Integration tests of PasskeyChallengeDB.
*
* These tests require a local Dynamo DB service to be available. See
* [[./local-dev/README.md#setting-up-passkeys-tables Setting up Passkeys tables]]
*/
class PasskeyChallengeDBTest extends AnyFreeSpec with Matchers {
"test db stuff - use this to test DynamoDB stuff locally during development" - {
given dynamoDB: DynamoDbClient = Clients.localDb
"insertion and querying" - {
val clock = Clock.fixed(Instant.parse("2025-03-31T01:00:00Z"), UTC)
val userChallenge = UserChallenge(
UserIdentity(
sub = "",
email = "test.user@example.com",
firstName = "Test",
lastName = "User",
exp = 0,
avatarUrl = None
),
Registration,
new DefaultChallenge("challenge".getBytes(UTF_8)),
clock.instant.plusSeconds(60)
)
"insertion succeeds" ignore {
insert(userChallenge) match {
case Failure(e) =>
fail(s"Failed to insert user challenge: ${e.getMessage}")
case Success(_) => succeed
}
}
"load succeeds" ignore {
(for {
response <- loadChallenge(userChallenge.user, Authentication)
challenge <- extractChallenge(response, userChallenge.user)
} yield challenge) match {
case Failure(e) =>
fail(s"Failed to load user challenge: ${e.getMessage}")
case Success(challenge) =>
challenge.getValue shouldBe userChallenge.challenge.getValue
}
}
"deletion succeeds" ignore {
delete(userChallenge.user, Registration) match {
case Failure(e) =>
fail(s"Failed to delete user challenge: ${e.getMessage}")
case Success(_) => succeed
}
}
}
"create table" ignore {
createTable()
}
"destroy table" ignore {
destroyTable()
}
}
/** NB: Only use these for local testing use the provided CloudFormation
* template to create table in AWS environments.
*
* If you update this then be sure to also update the CloudFormation
* template's definition.
*/
private[aws] def createTable()(implicit
dynamoDB: DynamoDbClient
): CreateTableResponse = {
dynamoDB.createTable(
CreateTableRequest
.builder()
.tableName(tableName)
.keySchema(
KeySchemaElement
.builder()
.attributeName("username")
.keyType(HASH)
.build(),
KeySchemaElement
.builder()
.attributeName("flow")
.keyType(RANGE)
.build()
)
.attributeDefinitions(
AttributeDefinition
.builder()
.attributeName("username")
.attributeType(S)
.build(),
AttributeDefinition
.builder()
.attributeName("flow")
.attributeType(S)
.build()
)
.provisionedThroughput(
ProvisionedThroughput
.builder()
.readCapacityUnits(15L)
.writeCapacityUnits(15L)
.build()
)
.build()
)
}
private[aws] def destroyTable()(implicit
dynamoDb: DynamoDbClient
): DeleteTableResponse =
dynamoDb.deleteTable(
DeleteTableRequest
.builder()
.tableName(tableName)
.build()
)
}