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
|
import json
from tests import SiteTest, app
class EmptyDatabaseEndpointTests(SiteTest):
def test_api_docs_get_all(self):
response = self.client.get(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert200(response)
self.assertIsInstance(response.json, list)
def test_fetching_single_entry_returns_404(self):
response = self.client.get(
'/bot/bigbrother?user_id=01932',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert404(response)
self.assertIsInstance(response.json['error_message'], str)
class AddingAnEntryEndpointTests(SiteTest):
GOOD_DATA = {
'user_id': '42',
'channel_id': '55'
}
GOOD_DATA_JSON = json.dumps(GOOD_DATA)
def setUp(self):
response = self.client.post(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER'],
data=self.GOOD_DATA_JSON
)
self.assertEqual(response.status_code, 204)
def test_entry_is_in_all_entries(self):
response = self.client.get(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert200(response)
self.assertIn(self.GOOD_DATA, response.json)
def test_can_fetch_entry_with_param_lookup(self):
response = self.client.get(
f'/bot/bigbrother?user_id={self.GOOD_DATA["user_id"]}',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert200(response)
self.assertEqual(response.json, self.GOOD_DATA)
class UpdatingAnEntryEndpointTests(SiteTest):
ORIGINAL_DATA = {
'user_id': '300',
'channel_id': '400'
}
ORIGINAL_DATA_JSON = json.dumps(ORIGINAL_DATA)
UPDATED_DATA = {
'user_id': '300',
'channel_id': '500'
}
UPDATED_DATA_JSON = json.dumps(UPDATED_DATA)
def setUp(self):
response = self.client.post(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER'],
data=self.ORIGINAL_DATA_JSON
)
self.assertEqual(response.status_code, 204)
def test_can_update_data(self):
response = self.client.post(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER'],
data=self.UPDATED_DATA_JSON
)
self.assertEqual(response.status_code, 204)
class DeletingAnEntryEndpointTests(SiteTest):
SAMPLE_DATA = {
'user_id': '101',
'channel_id': '202'
}
SAMPLE_DATA_JSON = json.dumps(SAMPLE_DATA)
def setUp(self):
response = self.client.post(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER'],
data=self.SAMPLE_DATA_JSON
)
self.assertEqual(response.status_code, 204)
def test_delete_entry_returns_204(self):
response = self.client.delete(
f'/bot/bigbrother?user_id={self.SAMPLE_DATA["user_id"]}',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assertEqual(response.status_code, 204)
class SchemaValidationTests(SiteTest):
def test_get_with_invalid_user_id_param_returns_400(self):
response = self.client.get(
'/bot/bigbrother?user_id=lemon-is-not-a-number',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert400(response)
self.assertIsInstance(response.json['error_message'], str)
def test_post_with_invalid_data_returns_400(self):
bad_data_json = json.dumps({
'user_id': "I'M A NUMBER I SWEAR",
'channel_id': '42'
})
response = self.client.post(
'/bot/bigbrother',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER'],
data=bad_data_json
)
self.assert400(response)
self.assertIsInstance(response.json['error_message'], str)
def test_delete_with_invalid_user_id_param_returns_400(self):
response = self.client.delete(
'/bot/bigbrother?user_id=totally-a-valid-number',
app.config['API_SUBDOMAIN'],
headers=app.config['TEST_HEADER']
)
self.assert400(response)
self.assertIsInstance(response.json['error_message'], str)
|