-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject_test.exs
More file actions
360 lines (281 loc) · 8.95 KB
/
Copy pathproject_test.exs
File metadata and controls
360 lines (281 loc) · 8.95 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
defmodule ImpactTracker.ProjectTest do
use ImpactTracker.DataCase
alias Ecto.Changeset
alias ImpactTracker.Project
describe "v2_changeset/2" do
setup do
%{data: build_project_data()}
end
test "returns a valid changeset", %{data: data} do
%{"cleartext_uuid" => cleartext_uuid, "hashed_uuid" => hashed_uuid} = data
changeset = %Project{} |> Project.v2_changeset(data)
assert %Changeset{valid?: true, changes: changes} = changeset
assert(
%{
cleartext_uuid: ^cleartext_uuid,
hashed_uuid: ^hashed_uuid,
no_of_active_users: 7,
no_of_users: 10
} = changes
)
assert changes.workflows |> Enum.count() == 2
end
test "creates the associated workflows", %{data: data} do
changeset = %Project{} |> Project.v2_changeset(data)
%{changes: %{workflows: workflows}} = changeset
assert [workflow | [_other_workflow]] = workflows
%{changes: changes} = workflow
assert changes |> Map.has_key?(:no_of_active_jobs)
end
test "validates the presence of hashed_uuid", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> remove_data("hashed_uuid"))
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
hashed_uuid: {"can't be blank", [{:validation, :required}]}
] = errors
)
end
test "is valid even if cleartext_uuid is absent", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> remove_data("cleartext_uuid"))
assert %Changeset{valid?: true} = changeset
end
test "validates the presence of no_of_users", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> remove_data("no_of_users"))
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_users: {"can't be blank", [{:validation, :required}]}
] = errors
)
end
test "validates that no_of_users >= 0", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_users", -1))
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_users: {
"must be greater than or equal to %{number}",
[
{:validation, :number},
{:kind, :greater_than_or_equal_to},
{:number, 0}
]
}
] = errors
)
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_users", 0))
assert %Changeset{valid?: true} = changeset
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_users", 1))
assert %Changeset{valid?: true} = changeset
end
test "validates the presence of no_of_active_users", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> remove_data("no_of_active_users"))
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_active_users: {"can't be blank", [{:validation, :required}]}
] = errors
)
end
test "validates that no_of_active_users >= 0", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_active_users", -1))
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_active_users: {
"must be greater than or equal to %{number}",
[
{:validation, :number},
{:kind, :greater_than_or_equal_to},
{:number, 0}
]
}
] = errors
)
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_active_users", 0))
assert %Changeset{valid?: true} = changeset
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("no_of_active_users", 1))
assert %Changeset{valid?: true} = changeset
end
test "validate hashed_uuid is a hash of cleartext", %{data: data} do
changeset =
%Project{}
|> Project.v2_changeset(data |> modify_data("hashed_uuid", hash("foo")))
assert %Changeset{valid?: false, errors: errors} = changeset
assert [hashed_uuid: {"is not a hash of cleartext uuid", []}] = errors
end
test "validate hashed_uuid format if cleartext absent", %{data: data} do
with_correct_hash =
data
|> remove_clear_set_hashed(correct_format_hash())
changeset =
%Project{}
|> Project.v2_changeset(with_correct_hash)
assert %Changeset{valid?: true} = changeset
truncated_hash =
data
|> remove_clear_set_hashed(short_1_char_hash())
%Project{}
|> Project.v2_changeset(truncated_hash)
|> assert_incorrectly_formatted_hash
extended_hash =
data
|> remove_clear_set_hashed(extra_1_char_hash())
%Project{}
|> Project.v2_changeset(extended_hash)
|> assert_incorrectly_formatted_hash
bad_chars_hash =
data
|> remove_clear_set_hashed(non_alphanum_char_hash())
%Project{}
|> Project.v2_changeset(bad_chars_hash)
|> assert_incorrectly_formatted_hash
end
end
describe "v3_changeset/2" do
setup do
%{data: build_v3_project_data()}
end
test "returns a valid changeset including no_of_monthly_active_users", %{
data: data
} do
changeset = %Project{} |> Project.v3_changeset(data)
assert %Changeset{valid?: true, changes: changes} = changeset
assert(
%{
no_of_active_users: 7,
no_of_monthly_active_users: 4,
no_of_users: 10
} = changes
)
assert changes.workflows |> Enum.count() == 2
end
test "validates the presence of no_of_monthly_active_users", %{data: data} do
changeset =
%Project{}
|> Project.v3_changeset(
data
|> remove_data("no_of_monthly_active_users")
)
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_monthly_active_users:
{"can't be blank", [{:validation, :required}]}
] = errors
)
end
test "validates that no_of_monthly_active_users >= 0", %{data: data} do
changeset =
%Project{}
|> Project.v3_changeset(
data
|> modify_data("no_of_monthly_active_users", -1)
)
assert %Changeset{valid?: false, errors: errors} = changeset
assert(
[
no_of_monthly_active_users: {
"must be greater than or equal to %{number}",
[
{:validation, :number},
{:kind, :greater_than_or_equal_to},
{:number, 0}
]
}
] = errors
)
changeset =
%Project{}
|> Project.v3_changeset(
data
|> modify_data("no_of_monthly_active_users", 0)
)
assert %Changeset{valid?: true} = changeset
end
end
defp build_project_data do
uuid = generate_uuid()
%{
"cleartext_uuid" => uuid,
"hashed_uuid" => hash(uuid),
"no_of_active_users" => 7,
"no_of_users" => 10,
"workflows" => build_workflow_data()
}
end
defp build_v3_project_data do
build_project_data() |> Map.put("no_of_monthly_active_users", 4)
end
defp build_workflow_data do
uuid_1 = generate_uuid()
uuid_2 = generate_uuid()
[
%{
"cleartext_uuid" => uuid_1,
"hashed_uuid" => hash(uuid_1),
"no_of_active_jobs" => 0,
"no_of_jobs" => 1,
"no_of_runs" => 2,
"no_of_steps" => 3
},
%{
"cleartext_uuid" => uuid_2,
"hashed_uuid" => hash(uuid_2),
"no_of_active_jobs" => 3,
"no_of_jobs" => 4,
"no_of_runs" => 5,
"no_of_steps" => 6
}
]
end
defp modify_data(data, key, value) do
data |> Map.merge(%{key => value})
end
defp remove_data(data, key) do
data |> Map.delete(key)
end
defp remove_clear_set_hashed(data, hashed_uuid) do
data
|> remove_data("cleartext_uuid")
|> Map.merge(%{"hashed_uuid" => hashed_uuid})
end
defp generate_uuid do
Ecto.UUID.generate()
end
defp hash(uuid), do: Base.encode16(:crypto.hash(:sha256, uuid))
defp correct_format_hash, do: String.duplicate("a1Db", 16)
defp short_1_char_hash, do: String.duplicate("a1D", 21)
defp extra_1_char_hash, do: String.duplicate("a1dB", 16) <> "x"
defp non_alphanum_char_hash, do: String.duplicate("a1D", 21) <> "="
defp assert_incorrectly_formatted_hash(changeset) do
assert %Changeset{valid?: false, errors: errors} = changeset
assert [
hashed_uuid: {
"does not appear to be valid SHA256",
[validation: :format]
}
] = errors
end
end