aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/functional/test_notifications.py
blob: a604fc44da69e319c57284165cca1603b9414238 (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
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
from datetime import datetime, timezone

import pytest
from flask import url_for
from flask.testing import FlaskClient
from pytest_mock import MockerFixture

from sec_certs_page import mongo


@pytest.fixture()
def certificate(mongo_data):
    return mongo.db.cc.find_one({"_id": "6ca52f5450bedb2f"})


def test_subscribe_changes(request, user, logged_in: FlaskClient, mocker: MockerFixture, certificate):
    user, password = user
    mocker.patch("flask_wtf.csrf.validate_csrf")
    sub_obj = {
        "cert": {
            "name": certificate["name"],
            "hashid": certificate["dgst"],
            "type": "cc",
            "url": url_for("cc.entry", hashid=certificate["dgst"]),
        },
        "updates": "all",
    }
    resp = logged_in.post("/notify/subscribe/", json=sub_obj)
    assert resp.status_code == 200
    assert resp.is_json
    assert resp.json == {"status": "OK"}

    sub = mongo.db.subs.find_one({"username": user.username})
    request.addfinalizer(lambda: mongo.db.subs.delete_one({"_id": sub["_id"]}))
    assert sub
    assert sub["timestamp"]
    assert sub["updates"] == "all"
    assert sub["type"] == "changes"
    assert sub["certificate"]["name"] == certificate["name"]


def test_subscribe_new(request, user, logged_in: FlaskClient, mocker: MockerFixture):
    user, password = user
    mocker.patch("flask_wtf.csrf.validate_csrf")
    sub_obj_new = {
        "which": "cc",
    }
    resp = logged_in.post("/notify/subscribe/new/", json=sub_obj_new)
    assert resp.status_code == 200
    assert resp.is_json
    assert resp.json["status"] == "OK"

    sub = mongo.db.subs.find_one({"username": user.username})
    request.addfinalizer(lambda: mongo.db.subs.delete_one({"_id": sub["_id"]}))
    assert sub
    assert sub["timestamp"]
    assert sub["type"] == "new"
    assert sub["which"] == "cc"


def test_bad_subscribe(user, logged_in: FlaskClient, mocker: MockerFixture):
    mocker.patch("flask_wtf.csrf.validate_csrf")
    resp = logged_in.post("/notify/subscribe/", json={"bad": "keys"})
    assert resp.status_code == 400
    resp = logged_in.post("/notify/subscribe/", json={"cert": "...", "updates": "bad"})
    assert resp.status_code == 400
    resp = logged_in.post(
        "/notify/subscribe/",
        json={"cert": "...", "updates": "all"},
    )
    assert resp.status_code == 400
    resp = logged_in.post(
        "/notify/subscribe/",
        json={"cert": {"a": "bad"}, "updates": "all"},
    )
    assert resp.status_code == 400
    resp = logged_in.post(
        "/notify/subscribe/",
        json={"cert": {"name": "...", "hashid": "...", "url": "...", "type": "bad"}, "updates": "all"},
    )
    assert resp.status_code == 400


def test_bad_subscribe_new(user, logged_in: FlaskClient, mocker: MockerFixture):
    mocker.patch("flask_wtf.csrf.validate_csrf")
    resp = logged_in.post("/notify/subscribe/new/", json="aaa")
    assert resp.status_code == 400
    resp = logged_in.post("/notify/subscribe/", json={"bad": "keys"})
    assert resp.status_code == 400
    resp = logged_in.post(
        "/notify/subscribe/new/",
        json={"which": "bad"},
    )
    assert resp.status_code == 400


@pytest.fixture(scope="function")
def subscription(request, user, certificate):
    user, password = user
    sub = {
        "timestamp": datetime.now(timezone.utc),
        "username": user.username,
        "type": "changes",
        "updates": "all",
        "certificate": {
            "name": certificate["name"],
            "hashid": certificate["dgst"],
            "type": "cc",
            "url": url_for("cc.entry", hashid=certificate["dgst"]),
        },
    }
    res = mongo.db.subs.insert_one(sub)
    sub["_id"] = res.inserted_id
    try:
        yield sub
    finally:
        mongo.db.subs.delete_one({"_id": res.inserted_id})


def test_manage(user, logged_in: FlaskClient, mocker: MockerFixture, subscription):
    user, password = user
    mocker.patch("flask_wtf.csrf.validate_csrf")
    resp = logged_in.get(f"/notify/manage/")
    assert resp.status_code == 200

    data = {
        "new-0-which": "cc",
        "new-0-subscribe": "y",
        "new-1-which": "fips",
        "new-1-subscribe": "y",
        "new-2-which": "pp",
        "new-2-subscribe": "y",
    }
    resp = logged_in.post(f"/notify/manage/", data=data, follow_redirects=True)
    assert resp.status_code == 200
    subs = list(mongo.db.subs.find({"username": user.username}))
    assert len(subs) == 4
    assert all(s["type"] == "new" for s in subs if s["_id"] != subscription["_id"])

    data = {
        "new-0-which": "cc",
        "new-0-subscribe": "",
        "new-1-which": "fips",
        "new-1-subscribe": "",
        "new-2-which": "pp",
        "new-2-subscribe": "",
    }
    resp = logged_in.post(f"/notify/manage/", data=data, follow_redirects=True)
    assert resp.status_code == 200
    subs = list(mongo.db.subs.find({"username": user.username}))
    assert len(subs) == 1
    assert subs[0]["_id"] == subscription["_id"]

    data = {
        "changes-0-certificate_type": subscription["certificate"]["type"],
        "changes-0-certificate_hashid": subscription["certificate"]["hashid"],
        "changes-0-updates": "vuln",
        "changes-0-subscribe": "y",
    }
    resp = logged_in.post(f"/notify/manage/", data=data, follow_redirects=True)
    assert resp.status_code == 200
    sub = mongo.db.subs.find_one({"_id": subscription["_id"]})
    assert sub["updates"] == "vuln"


def test_unsubscribe_single(logged_in: FlaskClient, mocker: MockerFixture, subscription):
    mocker.patch("flask_wtf.csrf.validate_csrf")
    resp = logged_in.post(f"/notify/unsubscribe/", json={"id": str(subscription["_id"])}, follow_redirects=True)
    assert resp.status_code == 200
    sub = list(mongo.db.subs.find({"_id": subscription["_id"]}))
    assert not sub


def test_unsubscribe_all(user, logged_in: FlaskClient, mocker: MockerFixture, subscription):
    user, password = user
    mocker.patch("flask_wtf.csrf.validate_csrf")
    resp = logged_in.post(f"/notify/unsubscribe/all/", follow_redirects=True)
    assert resp.status_code == 200
    sub = list(mongo.db.subs.find({"username": user.username}))
    assert not sub