aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/functional/cc/test_diffs.py
blob: 7496920ad666f30629e422eb259a173a82e709f3 (plain) (blame)
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
import random

import pymongo
import pytest

from sec_certs_page import mongo
from sec_certs_page.cc.tasks import CCNotifier
from sec_certs_page.common.diffs import apply_explicit_diff, cc_diff_method, render_compare
from sec_certs_page.common.objformats import freeze, load


def test_cc_diff_renders(client):
    all_diffs = list(mongo.db.cc_diff.find({}, {"_id": True}))
    notifier = CCNotifier()
    for _ in range(100):
        id = random.choice(all_diffs)
        diff = load(mongo.db.cc_diff.find_one(id))
        cert = load(mongo.db.cc.find_one(diff["dgst"]))
        assert notifier.render_diff(id["_id"], cert, diff) is not None


def test_cc_compare_render(client):
    all_ids = list(mongo.db.cc.find({}, {"_id": True}))
    for _ in range(200):
        idd_one = random.choice(all_ids)
        cert_one = load(mongo.db.cc.find_one(idd_one))
        idd_other = random.choice(all_ids)
        cert_other = load(mongo.db.cc.find_one(idd_other))
        assert render_compare(cert_one, cert_other, cc_diff_method) is not None


@pytest.mark.parametrize("dgst", ["1412d1d9e0d553c1", "44f677892bb84ce5", "f1174ac2e100bc5c", "f0c22e3e4abad667"])
def test_cc_diff_reconstruction(dgst):
    new_diff = mongo.db.cc_diff.find_one({"dgst": dgst, "type": "new"})
    change_diffs = mongo.db.cc_diff.find({"dgst": dgst, "type": "change"}).sort([("timestamp", pymongo.ASCENDING)])

    current = load(new_diff["diff"])
    for diff in change_diffs:
        current = apply_explicit_diff(current, load(diff["diff"]))
    current = freeze(current)

    actual = load(mongo.db.cc.find_one({"dgst": dgst}))
    assert dict(current) == dict(actual)