-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuditTrailDBTest.scala
143 lines (132 loc) · 3.86 KB
/
AuditTrailDBTest.scala
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
142
143
package aws
import com.gu.janus.model.{AuditLog, JConsole}
import org.joda.time.{DateTime, DateTimeZone, Duration}
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._
class AuditTrailDBTest extends AnyFreeSpec with Matchers {
"test db stuff - use this to test DynamoDB stuff locally during development" - {
implicit val dynamoDB: DynamoDbClient = Clients.localDb
"insertion and querying" ignore {
val table = AuditTrailDB.getTable()
val dateTime: DateTime =
new DateTime(2015, 11, 5, 17, 35, DateTimeZone.UTC)
val al = AuditLog(
"account",
"username",
dateTime,
new Duration(3600000),
"accessLevel",
JConsole,
external = true
)
AuditTrailDB.insert(table, al)
val accountResults = AuditTrailDB.getAccountLogs(
table,
"account",
dateTime.minusDays(1),
dateTime.plusDays(1)
)
println(accountResults.toList)
val userResults = AuditTrailDB.getUserLogs(
table,
"username",
dateTime.minusDays(1),
dateTime.plusDays(1)
)
println(userResults.toList)
}
"create database 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 = {
val auditTableCreateRequest = CreateTableRequest
.builder()
.tableName(AuditTrailDB.tableName)
.keySchema(
KeySchemaElement
.builder()
.attributeName("j_account")
.keyType(HASH)
.build(),
KeySchemaElement
.builder()
.attributeName("j_timestamp")
.keyType(RANGE)
.build()
)
.attributeDefinitions(
AttributeDefinition
.builder()
.attributeName("j_account")
.attributeType("S")
.build(),
AttributeDefinition
.builder()
.attributeName("j_timestamp")
.attributeType("N")
.build(),
AttributeDefinition
.builder()
.attributeName("j_username")
.attributeType("S")
.build()
)
.globalSecondaryIndexes(
GlobalSecondaryIndex
.builder()
.indexName("AuditTrailByUser")
.keySchema(
KeySchemaElement
.builder()
.attributeName("j_username")
.keyType(HASH)
.build(),
KeySchemaElement
.builder()
.attributeName("j_timestamp")
.keyType(RANGE)
.build()
)
.projection(Projection.builder().projectionType("ALL").build())
.provisionedThroughput(
ProvisionedThroughput
.builder()
.readCapacityUnits(15L)
.writeCapacityUnits(15L)
.build()
)
.build()
)
.provisionedThroughput(
ProvisionedThroughput
.builder()
.readCapacityUnits(15L)
.writeCapacityUnits(15L)
.build()
)
.build()
dynamoDB.createTable(auditTableCreateRequest)
}
private[aws] def destroyTable()(implicit
dynamoDB: DynamoDbClient
): DeleteTableResponse = {
val request =
DeleteTableRequest.builder().tableName(AuditTrailDB.tableName).build()
dynamoDB.deleteTable(request)
}
}