Projekt

Obecné

Profil

Stáhnout (11.9 KB) Statistiky
| Větev: | Tag: | Revize:
1
def test_import(server):
2
    assert 1 + 2 + 3 + 4 != -1 / 12
3

    
4
    ret = server.get("/")
5
    assert ret.status_code == 200
6
    assert str(ret.data, encoding='utf-8') == "Welcome to the X.509 management application homepage!"
7

    
8

    
9
def test_root_ca(server):
10
    ret = server.post("/api/certificates", content_type="application/json", json={
11
        "subject": {
12
            "C": "CZ",
13
            "CN": "Root CA s.r.o.",
14
            "L": "Pilsen",
15
            "O": "Root CA s.r.o.",
16
            "OU": "IT department",
17
            "ST": "Pilsen Region",
18
            "emailAddress": "root@ca.com"
19
        },
20
        "usage": {
21
            "CA": True,
22
            "SSL": True,
23
            "authentication": True,
24
            "digitalSignature": True
25
        },
26
        "validityDays": 30
27
    })
28

    
29
    assert ret.status_code == 201
30

    
31
    d = ret.json
32

    
33
    assert "data" in d
34
    assert d["data"] == 1
35
    assert "success" in d
36
    assert d["success"]
37

    
38

    
39
def test_invalid_params_1(server):
40
    ret = server.post("/api/certificates", content_type="application/json", json={
41
        "subject": {
42
            "C": "CZ",
43
            "CN": "Root CA s.r.o.",
44
            "L": "Pilsen",
45
            "O": "Root CA s.r.o.",
46
            "OU": "IT department",
47
            "ST": "Pilsen Region",
48
            "emailAddress": "root@ca.com"
49
        },
50
        "validityDays": 30
51
    })
52

    
53
    assert ret.status_code == 400
54

    
55
    d = ret.json
56

    
57
    assert "data" in d
58
    assert d["data"] == "Invalid request, missing parameters."
59
    assert "success" in d
60
    assert not d["success"]
61

    
62

    
63
def test_invalid_params_2(server):
64
    ret = server.post("/api/certificates", content_type="application/json", json={
65
        "subject": {
66
            "C": "CZ",
67
            "CN": "Root CA s.r.o.",
68
            "L": "Pilsen",
69
            "O": "Root CA s.r.o.",
70
            "OU": "IT department",
71
            "ST": "Pilsen Region",
72
            "emailAddress": "root@ca.com"
73
        },
74
        "usage": {
75
            "CA": True,
76
            "SSL": True,
77
            "authentication": True,
78
            "digitalSignature": True
79
        },
80
        "validityDays": "tak mesic treba plus minus"
81
    })
82

    
83
    assert ret.status_code == 400
84

    
85
    d = ret.json
86

    
87
    assert "data" in d
88
    assert d["data"] == "Invalid request, wrong parameters."
89
    assert "success" in d
90
    assert not d["success"]
91

    
92

    
93
def test_invalid_params_3(server):
94
    ret = server.post("/api/certificates", content_type="application/json", json={
95
        "subject": {
96
            "C": "CZ",
97
            "CN": "Root CA s.r.o.",
98
            "L": "Pilsen",
99
            "O": "Root CA s.r.o.",
100
            "OU": "IT department",
101
            "ST": ["Bosna", "Herzegovina"],
102
            "emailAddress": "root@ca.com"
103
        },
104
        "usage": {
105
            "CA": True,
106
            "SSL": True,
107
            "authentication": True,
108
            "digitalSignature": True
109
        },
110
        "validityDays": 30
111
    })
112

    
113
    assert ret.status_code == 400
114

    
115
    d = ret.json
116

    
117
    assert "data" in d
118
    assert d["data"] == "Invalid request, wrong parameters."
119
    assert "success" in d
120
    assert not d["success"]
121

    
122

    
123
def test_invalid_params_4(server):
124
    ret = server.post("/api/certificates", content_type="application/json", json={
125
        "CA": 80081358008135,
126
        "subject": {
127
            "C": "CZ",
128
            "CN": "Root CA s.r.o.",
129
            "L": "Pilsen",
130
            "O": "Root CA s.r.o.",
131
            "OU": "IT department",
132
            "ST": "Pilsen",
133
            "emailAddress": "root@ca.com"
134
        },
135
        "usage": {
136
            "CA": True,
137
            "SSL": True,
138
            "authentication": True,
139
            "digitalSignature": True
140
        },
141
        "validityDays": 30
142
    })
143

    
144
    assert ret.status_code == 400
145

    
146
    d = ret.json
147

    
148
    assert "data" in d
149
    assert d["data"] == "No certificate authority with such unique ID exists."
150
    assert "success" in d
151
    assert not d["success"]
152

    
153

    
154
def test_create_chain(server):
155
    ret = server.post("/api/certificates", content_type="application/json", json={
156
        "subject": {
157
            "C": "CZ",
158
            "CN": "Another Root CA s.r.o.",
159
            "L": "Pilsen",
160
            "O": "Another Root CA s.r.o.",
161
            "OU": "IT department",
162
            "ST": "Pilsen Region",
163
            "emailAddress": "root@ca.com"
164
        },
165
        "usage": {
166
            "CA": True,
167
            "SSL": True,
168
            "authentication": True,
169
            "digitalSignature": True
170
        },
171
        "validityDays": 30
172
    })
173

    
174
    assert ret.status_code == 201
175

    
176
    d = ret.json
177

    
178
    assert "data" in d
179
    assert d["data"] == 2
180
    assert "success" in d
181
    assert d["success"]
182

    
183
    ret = server.post("/api/certificates", content_type="application/json", json={
184
        "CA": 2,
185
        "subject": {
186
            "C": "CZ",
187
            "CN": "Intermediate CA s.r.o.",
188
            "L": "Pilsen",
189
            "O": "Intermediate CA s.r.o.",
190
            "OU": "IT department",
191
            "ST": "Pilsen Region",
192
            "emailAddress": "inter@ca.com"
193
        },
194
        "usage": {
195
            "CA": True,
196
            "SSL": True,
197
            "authentication": True,
198
            "digitalSignature": True
199
        },
200
        "validityDays": 30
201
    })
202

    
203
    assert ret.status_code == 201
204

    
205
    d = ret.json
206

    
207
    assert "data" in d
208
    assert d["data"] == 3
209
    assert "success" in d
210
    assert d["success"]
211

    
212
    ret = server.post("/api/certificates", content_type="application/json", json={
213
        "CA": 3,
214
        "subject": {
215
            "C": "CZ",
216
            "CN": "Legit end certificate s.r.o.",
217
            "L": "Pilsen",
218
            "O": "I swear s.r.o.",
219
            "OU": "IT department",
220
            "ST": "Pilsen Region",
221
            "emailAddress": "end@ca.com"
222
        },
223
        "usage": {
224
            "CA": True,
225
            "SSL": True,
226
            "authentication": True,
227
            "digitalSignature": True
228
        },
229
        "validityDays": 30
230
    })
231

    
232
    assert ret.status_code == 201
233

    
234
    d = ret.json
235

    
236
    assert "data" in d
237
    assert d["data"] == 4
238
    assert "success" in d
239
    assert d["success"]
240

    
241

    
242
def test_list_of_certificates(server):
243
    ret = server.get("/api/certificates")
244

    
245
    assert ret.status_code == 200
246

    
247
    usage_dict = {'CA': True, 'SSL': True, 'authentication': True, 'digitalSignature': True}
248
    s1 = "Root CA s.r.o."
249
    s2 = "Another Root CA s.r.o."
250
    s3 = "Intermediate CA s.r.o."
251

    
252
    assert "data" in ret.json
253
    assert "success" in ret.json
254
    assert ret.json["success"]
255

    
256
    d = ret.json["data"]
257

    
258
    # print(d)
259

    
260
    assert d[0]['CN'] == s1
261
    assert d[1]['CN'] == s2
262
    assert d[2]['CN'] == s3
263

    
264
    assert all(d[i]['id'] == i + 1 for i in range(3))
265
    assert all(d[i]['usage'] == usage_dict for i in range(3))
266

    
267
    assert d[0]['issuer']['CN'] == s1
268
    assert d[1]['issuer']['CN'] == s2
269
    assert d[2]['issuer']['CN'] == s2
270

    
271
    assert d[0]['issuer']['id'] == 1
272
    assert d[1]['issuer']['id'] == 2
273
    assert d[2]['issuer']['id'] == 2
274

    
275

    
276
def test_sign_by_non_ca(server):
277
    ret = server.post("/api/certificates", content_type="application/json", json={
278
        "CA": 2,
279
        "subject": {
280
            "C": "CZ",
281
            "CN": "Fake Intermediate CA s.r.o.",
282
            "L": "Pilsen",
283
            "O": "Fake Intermediate CA s.r.o.",
284
            "OU": "IT department",
285
            "ST": "Pilsen Region",
286
            "emailAddress": "inter@ca.com"
287
        },
288
        "usage": {
289
            "CA": False,
290
            "SSL": True,
291
            "authentication": True,
292
            "digitalSignature": True
293
        },
294
        "validityDays": 30
295
    })
296

    
297
    assert ret.status_code == 201
298

    
299
    d = ret.json
300

    
301
    assert "data" in d
302
    assert d["data"] == 5
303
    assert "success" in d
304
    assert d["success"]
305

    
306
    ret = server.post("/api/certificates", content_type="application/json", json={
307
        "CA": 5,
308
        "subject": {
309
            "C": "CZ",
310
            "CN": "End certificate signed by end certificate s.r.o.",
311
            "L": "Pilsen",
312
            "O": "This is probably bad s.r.o.",
313
            "OU": "IT department",
314
            "ST": "Pilsen Region",
315
            "emailAddress": "end@ca.com"
316
        },
317
        "usage": {
318
            "CA": False,
319
            "SSL": True,
320
            "authentication": True,
321
            "digitalSignature": True
322
        },
323
        "validityDays": 30
324
    })
325

    
326
    # TODO discussion -> assert ret.status_code == 400
327
    assert ret.status_code == 201
328

    
329
    d = ret.json
330

    
331
    assert "data" in d
332
    assert d["data"] == 6
333
    assert "success" in d
334
    assert d["success"]
335

    
336

    
337
def test_filtering(server):
338
    ret = server.get("/api/certificates", json={"filtering": {"CA": True}})
339

    
340
    assert ret.status_code == 200
341

    
342
    usage_dict = {'CA': True, 'SSL': True, 'authentication': True, 'digitalSignature': True}
343
    s1 = "Root CA s.r.o."
344
    s2 = "Another Root CA s.r.o."
345
    s3 = "Intermediate CA s.r.o."
346

    
347
    assert "data" in ret.json
348
    assert "success" in ret.json
349
    assert ret.json["success"]
350

    
351
    d = ret.json["data"]
352

    
353
    # print(d)
354

    
355
    assert d[0]['CN'] == s1
356
    assert d[1]['CN'] == s2
357
    assert d[2]['CN'] == s3
358

    
359
    assert all(d[i]['id'] == i + 1 for i in range(3))
360
    assert all(d[i]['usage'] == usage_dict for i in range(3))
361

    
362
    assert d[0]['issuer']['CN'] == s1
363
    assert d[1]['issuer']['CN'] == s2
364
    assert d[2]['issuer']['CN'] == s2
365

    
366
    assert d[0]['issuer']['id'] == 1
367
    assert d[1]['issuer']['id'] == 2
368
    assert d[2]['issuer']['id'] == 2
369

    
370

    
371
def test_get_one(server):
372
    ret = server.get("/api/certificates/1")
373

    
374
    assert ret.status_code == 200
375

    
376
    assert "data" in ret.json
377
    assert "success" in ret.json
378
    assert ret.json["success"]
379

    
380
    d = ret.json["data"]
381

    
382
    assert str(d).startswith("-----BEGIN CERTIFICATE-----")
383

    
384

    
385
def test_get_one_bad_param(server):
386
    ret = server.get("/api/certificates/whoopsie")
387

    
388
    assert ret.status_code == 400
389
    assert "data" in ret.json
390
    assert "success" in ret.json
391
    assert not ret.json["success"]
392
    assert ret.json["data"] == "Invalid request, wrong parameters."
393

    
394

    
395
def test_get_one_bad_param_2(server):
396
    ret = server.get("/api/certificates/805")
397

    
398
    assert ret.status_code == 205
399
    assert "data" in ret.json
400
    assert "success" in ret.json
401
    assert not ret.json["success"]
402
    assert ret.json["data"] == "No such certificate found."
403

    
404

    
405
def test_get_detes(server):
406
    original = {
407
        "CA": 1,
408
        "subject": {
409
            "C": "CZ",
410
            "CN": "Good boy end cert s.r.o.",
411
            "L": "Pilsen",
412
            "O": "This is most likely a good s.r.o.",
413
            "OU": "IT department",
414
            "ST": "Pilsen Region",
415
            "emailAddress": "end@ca.com"
416
        },
417
        "usage": {
418
            "CA": False,
419
            "SSL": True,
420
            "authentication": False,
421
            "digitalSignature": True
422
        },
423
        "validityDays": 30
424
    }
425
    ret = server.post("/api/certificates", content_type="application/json", json=original)
426

    
427
    assert ret.status_code == 201
428

    
429
    assert "data" in ret.json
430
    assert "success" in ret.json
431
    assert ret.json["success"]
432

    
433
    d = ret.json
434

    
435
    assert d["data"] == 7
436

    
437
    id = d["data"]
438

    
439
    ret = server.get(f"/api/certificates/{id}/details")
440

    
441
    assert ret.status_code == 200
442

    
443
    assert "data" in ret.json
444
    assert "success" in ret.json
445
    assert ret.json["success"]
446

    
447
    d = ret.json
448

    
449
    new = d["data"]
450

    
451
    assert original["CA"] == new["CA"]
452
    assert original["subject"] == new["subject"]
453
    assert original["usage"] == new["usage"]
454

    
455

    
456
def test_get_root(server):
457
    ret = server.get("/api/certificates/2")
458

    
459
    assert ret.status_code == 200
460

    
461
    assert "data" in ret.json
462
    assert "success" in ret.json
463
    assert ret.json["success"]
464

    
465
    original = ret.json["data"]
466

    
467
    ret = server.get("/api/certificates/4/root")
468

    
469
    assert ret.status_code == 200
470

    
471
    assert "data" in ret.json
472
    assert "success" in ret.json
473
    assert ret.json["success"]
474

    
475
    new = ret.json["data"]
476

    
477
    assert original == new
478

    
479

    
480
def test_get_root_invalid(server):
481
    ret = server.get("/api/certificates/805/root")
482

    
483
    assert ret.status_code == 205
484
    assert "data" in ret.json
485
    assert "success" in ret.json
486
    assert not ret.json["success"]
487
    assert ret.json["data"] == "No such certificate found."
488

    
(2-2/3)