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
import pytest
from flask import g
from sqlalchemy import select

from go_ji.db import Short, User


def test_up(client):
    response = client.get("/up")

    assert response.status_code == 200


class TestAuth:
    def test_authenticates(self, app):
        with app.test_request_context(
            "/",
            headers={
                "Tailscale-User-Login": "foo@example",
                "Tailscale-User-Name": "Foo Bar",
            },
        ):
            app.preprocess_request()

            assert g.user.name == "Foo Bar"
            assert g.user.login == "foo@example"

    def test_requires_tailscale_headers(self, client):
        response = client.get("/")

        assert response.status_code == 403

    def test_requires_tailscale_name(self, client):
        with pytest.raises(KeyError):
            client.get("/", headers={"Tailscale-User-Login": "foo@example"})

    def test_updates_name(self, app, client, db_session):
        db_session.add(User(login="foo@example", name="Foo Bar"))
        db_session.commit()

        with app.test_request_context("/"):
            client.get(
                "/",
                headers={
                    "Tailscale-User-Login": "foo@example",
                    "Tailscale-User-Name": "Bar Foo",
                },
            )

            assert g.user.name == "Bar Foo"

    def test_skips_if_already_checked(self, app, client):
        with app.test_request_context("/"):
            g.user = User(login="foo@example", name="Foo Bar")

            client.get(
                "/",
                headers={
                    "Tailscale-User-Login": "foo@example",
                    "Tailscale-User-Name": "Bar Foo",
                },
            )

            assert g.user.name == "Foo Bar"
            assert g.user.login == "foo@example"


class TestIndex:
    def test_index(self, authed, client):
        with authed:
            response = client.get("/")

            assert response.status_code == 200

    def test_shows_top_links(self, authed, client):
        with authed:
            response = client.get("/")
            assert b"foo</a>" not in response.data

            client.post("/links", data={"slug": "foo", "url": "https://example.com"})
            response = client.get("/")
            assert b"foo</a>" in response.data
            assert b"0" in response.data

            client.get("/foo", data={"slug": "foo", "url": "https://example.com"})
            response = client.get("/")
            assert b"foo</a>" in response.data
            assert b"1" in response.data


class TestCreateLink:
    def test_create_link(self, authed, client, db_session):
        with authed:
            response = client.post(
                "/links", data={"slug": "foo", "url": "https://example.com"}
            )

            assert response.status_code == 302
            assert response.headers["Location"] == "/"

            short = db_session.scalars(select(Short).where(Short.slug == "foo")).one()
            assert [l.url for l in short.longs] == ["https://example.com"]

    def test_requires_slug(self, authed, client):
        with authed:
            response = client.post("/links", data={"url": "https://example.com"})

            assert response.status_code == 400

    def test_requires_url(self, authed, client):
        with authed:
            response = client.post("/links", data={"slug": "foo"})

            assert response.status_code == 400

    def test_invalid_slug(self, authed, client):
        with authed:
            response = client.post(
                "/links", data={"slug": "", "url": "https://example.com"}
            )
            assert response.status_code == 400

            response = client.post(
                "/links", data={"slug": "!", "url": "https://example.com"}
            )
            assert response.status_code == 400

            response = client.post(
                "/links", data={"slug": "!a", "url": "https://example.com"}
            )
            assert response.status_code == 400

            response = client.post(
                "/links", data={"slug": "a!", "url": "https://example.com"}
            )
            assert response.status_code == 400


class TestGo:
    def test_go(self, authed, client):
        with authed:
            client.post("/links", data={"slug": "foo", "url": "https://example.com"})

            response = client.get("/foo")

            assert response.status_code == 302
            assert response.headers["Location"] == "https://example.com"

    def test_no_slug(self, authed, client):
        with authed:
            response = client.get("/foo")

            assert response.status_code == 404


def test_die(authed, client):
    with authed:
        with pytest.raises(ZeroDivisionError):
            client.get("/die")