aboutsummaryrefslogtreecommitdiff
path: root/tests/integration/wsgi.py
blob: 8124abe9f5480b2c63b7aa753e75dc0e8ff8b668 (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
from datetime import datetime, UTC
from unittest import TestCase

from werkzeug.http import parse_date

from components.database import subscriptions
from wsgi import app

class TestFlask(TestCase):
    def setUp(self) -> None:
        app.config['TESTING'] = True
        self.client = app.test_client()
        subscriptions.delete_many({})

    def test_add_sub(self) -> None:
        # A valid channel - should succeed.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
            'time_between_fetches': 3123,
        })
        self.assertEqual(response.status_code, 201)
        # Duplicate - should return 409.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
            'time_between_fetches': 532,
        })
        self.assertEqual(response.status_code, 409)
        # A madeup channel - should return 400.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk14Tg--mrJ2A",
            'time_between_fetches': 6824,
        })
        self.assertEqual(response.status_code, 400)
        # Integer strings are accepted for time_between_fetches.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/playlist?list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr",
            'time_between_fetches': "622",
        })
        self.assertEqual(response.status_code, 201)
        # An invalid duration - should return 400.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
            'time_between_fetches': "32.2",
        })
        self.assertEqual(response.status_code, 400)
        # An invalid subscription - should return 400.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/watch?v=WI4U1SVIO3I",
            'time_between_fetches': 132,
        })
        self.assertEqual(response.status_code, 400)
        # A valid channel - should succeed.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/@MentalOutlaw/videos",
            'time_between_fetches': 234,
        })
        self.assertEqual(response.status_code, 201)
        # A valid playlist - should succeed.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/watch?v=kYB8IZa5AuE&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=3",
            'time_between_fetches': 231,
        })
        self.assertEqual(response.status_code, 201)
        # A duplicate despite the structure of the url being different.
        response = self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab",
            'time_between_fetches': 2380,
        })
        self.assertEqual(response.status_code, 409)

    def test_subs_info(self) -> None:
        # Should be empty at start.
        response = self.client.get("/subs-info")
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        self.assertListEqual(response_data, [])
        # Fetching any individual subscription now should fail.
        response = self.client.get("/sub-info/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 404)
        # Fetching the videos of an individual subscription should also fail.
        response = self.client.get("/vid-from-link/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 404)
        # Now add some data to test on...
        self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
            'time_between_fetches': 1,
        })
        self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/playlist?list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr",
            'time_between_fetches': 1,
        })
        # Now should contain 2 subscriptions.
        response = self.client.get("/subs-info")
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        self.assertEqual(len(response_data), 2)
        for sub_info in response_data:
            self.assertEqual(sub_info["videos"], 0)
        # Now fetching an added subscriptions should work.
        response = self.client.get("/sub-info/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        self.assertEqual(response_data["_id"], "yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        # Same for its videos - though they should still be empty.
        response = self.client.get("/vid-from-link/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        self.assertListEqual(response_data, [])
        # Subscriptions not added should still not work.
        response = self.client.get("/sub-info/yt:channel:7YOGHUfC1Tb6E4pudI9STA")
        self.assertEqual(response.status_code, 404)
        # Same for for their videos.
        response = self.client.get("/vid-from-link/yt:channel:7YOGHUfC1Tb6E4pudI9STA")
        self.assertEqual(response.status_code, 404)

    def test_modify_subs(self) -> None:
        # Confirm all methods do not work before the item exists.
        response = self.client.patch("/set-time-between-fetches/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'time_between_fetches': 313,
        })
        self.assertEqual(response.status_code, 404)
        response = self.client.patch("/set-viewed/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'viewed_time': datetime.now(tz=UTC).isoformat(),
        })
        self.assertEqual(response.status_code, 404)
        response = self.client.delete("/delete-sub/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 404)
        # Now add some data to test on...
        self.client.post("/add-sub/", data={
            'url': "https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
            'time_between_fetches': 1,
        })
        # Test set-time-between-fetches.
        response = self.client.patch("/set-time-between-fetches/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'time_between_fetches': 31323,
        })
        self.assertEqual(response.status_code, 200)
        # Should not work if an integer is not provided.
        response = self.client.patch("/set-time-between-fetches/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'time_between_fetches': 31.3,
        })
        self.assertEqual(response.status_code, 400)
        # Test set-viewed.
        update_time_to = datetime.now(tz=UTC)
        response = self.client.patch("/set-viewed/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'viewed_time': update_time_to.isoformat(),
        })
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        self.assertEqual(update_time_to.replace(microsecond=0), parse_date(response_data["last_viewed"]))
        # Should not work with an invalid datetime string.
        response = self.client.patch("/set-viewed/yt:channel:Ba659QWEk1AI4Tg--mrJ2A", data={
            'viewed_time': "Monday 11th of February 2023",
        })
        self.assertEqual(response.status_code, 400)
        # Should work fine without a viewed_time set though...
        time_before_updating = datetime.now(tz=UTC)
        response = self.client.patch("/set-viewed/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 200)
        response_data = response.get_json()
        last_viewed = parse_date(response_data["last_viewed"])
        assert last_viewed
        self.assertLessEqual(time_before_updating.replace(microsecond=0), last_viewed)
        # Delete the subscription.
        response = self.client.delete("/delete-sub/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 200)
        # Make sure the item no longer exists.
        response = self.client.get("/sub-info/yt:channel:Ba659QWEk1AI4Tg--mrJ2A")
        self.assertEqual(response.status_code, 404)

    def tearDown(self) -> None:
        subscriptions.delete_many({})