From ec6f8154cd4df9f93266f3ea07aaf68e03e4dee1 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 22 Jul 2018 18:59:04 +0200 Subject: Testing is hard and I hate it. --- tests/test_api_bot_snake.py | 62 +++++++++++++++++++++----------- tests/test_api_bot_tags.py | 2 +- tests/test_clean_logs.py | 88 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_roots.py | 1 + 4 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 tests/test_clean_logs.py (limited to 'tests') diff --git a/tests/test_api_bot_snake.py b/tests/test_api_bot_snake.py index b5aa3bab..fcc18409 100644 --- a/tests/test_api_bot_snake.py +++ b/tests/test_api_bot_snake.py @@ -1,42 +1,64 @@ -import os +"""Tests the `/api/bot/snake_` endpoints.""" + from tests import SiteTest, app -class ApiBotSnakeEndpoints(SiteTest): - """ - Tests the following endpoints: - - snake_movies - - snake_quiz - - snake_names - - snake_idioms - - snake_facts - """ + +class TestSnakeFactsAPI(SiteTest): + """GET method - get snake fact""" def test_snake_facts(self): - # GET method - get snake fact - response = self.client.get('/bot/snake_facts', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + response = self.client.get( + '/bot/snake_facts', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json), str) + +class TestSnakeIdiomAPI(SiteTest): + """GET method - get snake idiom""" + def test_snake_idiom(self): - # GET method - get snake idiom - response = self.client.get('/bot/snake_idioms', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + response = self.client.get( + '/bot/snake_idioms', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json), str) + +class TestSnakeQuizAPI(SiteTest): + """GET method - get snake quiz""" + def test_snake_quiz(self): - # GET method - get snake quiz - response = self.client.get('/bot/snake_quiz', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + response = self.client.get( + '/bot/snake_quiz', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json), dict) + +class TestSnakeNameAPI(SiteTest): + """GET method - get a single snake name, or all of them.""" + def test_snake_names(self): - # GET method - get snake name - response = self.client.get('/bot/snake_names', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + response = self.client.get( + '/bot/snake_names', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json), dict) def test_snake_names_all(self): - # GET method - get all snake names - response = self.client.get('/bot/snake_names?get_all=True', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + response = self.client.get( + '/bot/snake_names?get_all=True', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json), list) diff --git a/tests/test_api_bot_tags.py b/tests/test_api_bot_tags.py index 66940f7e..fa06e0fa 100644 --- a/tests/test_api_bot_tags.py +++ b/tests/test_api_bot_tags.py @@ -1,7 +1,7 @@ -import os import json from tests import SiteTest, app + class ApiBotTagsEndpoint(SiteTest): def test_api_tags(self): diff --git a/tests/test_clean_logs.py b/tests/test_clean_logs.py new file mode 100644 index 00000000..fe0c5ac7 --- /dev/null +++ b/tests/test_clean_logs.py @@ -0,0 +1,88 @@ +"""Tests the `/api/bot/clean` endpoint.""" +import json + +from tests import SiteTest, app + + +class TestCleanLogAPI(SiteTest): + """ + Tests submitting a clean log and + verifies that we get a UUID in return. + + Then tests that + """ + + def test_returns_400_on_bad_data(self): + bad_data = json.dumps({ + "scubfire": "testiclaes" + }) + + response = self.client.post( + '/bot/clean', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'], + data=bad_data + ) + self.assert400(response) + + def test_submit_clean_log(self): + good_data = json.dumps({ + "log_data": [ + { + "author": "something", + "content": "testy", + "timestamp": "this way comes" + } + ] + }) + + response = self.client.post( + '/bot/clean', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'], + data=good_data + ) + + log_id = response.json.get("log_id") + + self.assert200(response) + self.assertIsNotNone(log_id) + self.assertGreater(len(log_id), 2) + self.assertEqual(type(log_id), str) + + +class TestCleanLogFrontEnd(SiteTest): + """ + Tests the frontend for + viewing the clean logs. + """ + + def test_clean_log_frontend_returns_200(self): + + # Get a log ID + good_data = json.dumps({ + "log_data": [ + { + "author": "something", + "content": "testy", + "timestamp": "this way comes" + } + ] + }) + + response = self.client.post( + '/bot/clean', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'], + data=good_data + ) + + log_id = response.json.get("log_id") + + # Now try to access it. + response = self.client.get( + f'/bot/clean_logs/{log_id}' + ) + + self.assert200(response) + self.assertIn("testy", response.text) \ No newline at end of file diff --git a/tests/test_roots.py b/tests/test_roots.py index 0b7b129c..1b270178 100644 --- a/tests/test_roots.py +++ b/tests/test_roots.py @@ -3,6 +3,7 @@ from pysite.constants import DISCORD_OAUTH_REDIRECT from pysite.constants import DISCORD_OAUTH_AUTHORIZED from pysite.constants import ERROR_DESCRIPTIONS + class RootEndpoint(SiteTest): """ Test cases for the root endpoint and error handling """ -- cgit v1.2.3 From 63d98bd2a052657773b3bf272b2249a45015a064 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 22 Jul 2018 19:43:27 +0200 Subject: fixed tests that were breaking. hooo-ey. --- tests/__init__.py | 1 - tests/test_clean_logs.py | 34 ++++++++-------------------------- tests/test_staff.py | 3 +-- 3 files changed, 9 insertions(+), 29 deletions(-) (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py index 84e69105..1f3ca1a4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,3 @@ -import json import os from flask import Blueprint diff --git a/tests/test_clean_logs.py b/tests/test_clean_logs.py index fe0c5ac7..210306c0 100644 --- a/tests/test_clean_logs.py +++ b/tests/test_clean_logs.py @@ -55,34 +55,16 @@ class TestCleanLogFrontEnd(SiteTest): """ Tests the frontend for viewing the clean logs. - """ - - def test_clean_log_frontend_returns_200(self): - - # Get a log ID - good_data = json.dumps({ - "log_data": [ - { - "author": "something", - "content": "testy", - "timestamp": "this way comes" - } - ] - }) - response = self.client.post( - '/bot/clean', - app.config['API_SUBDOMAIN'], - headers=app.config['TEST_HEADER'], - data=good_data - ) - - log_id = response.json.get("log_id") + Best I can do with our current + system is check if I'm redirected, + since this is behind OAuth. + """ - # Now try to access it. + def test_clean_log_frontend_returns_302(self): response = self.client.get( - f'/bot/clean_logs/{log_id}' + f'/bot/clean_logs/1', + 'http://pytest.local' ) - self.assert200(response) - self.assertIn("testy", response.text) \ No newline at end of file + self.assertEqual(response.status_code, 302) \ No newline at end of file diff --git a/tests/test_staff.py b/tests/test_staff.py index 68c182b5..bc911b0c 100644 --- a/tests/test_staff.py +++ b/tests/test_staff.py @@ -1,7 +1,6 @@ -import os -import json from tests import SiteTest, app + class StaffEndpoints(SiteTest): """ Test cases for staff subdomain """ -- cgit v1.2.3 From f967721b6e7acc827f5242e211d9a46a0f9aaddf Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 22 Jul 2018 21:13:09 +0200 Subject: Now doing role colorization instead of random colors for the cleanlog template. --- pysite/constants.py | 20 ++++++++++++++++++++ pysite/views/api/bot/clean.py | 3 ++- pysite/views/main/bot/cleanlog.py | 34 ++-------------------------------- tests/test_clean_logs.py | 5 +++-- 4 files changed, 27 insertions(+), 35 deletions(-) (limited to 'tests') diff --git a/pysite/constants.py b/pysite/constants.py index 69c73492..d7453871 100644 --- a/pysite/constants.py +++ b/pysite/constants.py @@ -101,6 +101,26 @@ JAM_QUESTION_TYPES = [ "slider" ] +# Server role colors +ROLE_COLORS = { + "owners": "#ffa3a1", + "admins": "#e76e6c", + "moderators": "#ce3c42", + "python": "#6f9fed", + "bots": "#6f9fed", + "admin bots": "#6f9fed", + "devops": "#a1d1ff", + "helpers": "#e0b000", + "subreddit moderators": "#d897ed", + "partners": "#b66fed", + "streamers": "#833cba", + "developers": "#fcfcfc", + "muted": "#fcfcfc", + "code jam champions": "#b108b4", + "jammers": "#258639", + "contributors": "#55cc6c" +} + # CSRF CSRF = CSRFProtect() diff --git a/pysite/views/api/bot/clean.py b/pysite/views/api/bot/clean.py index 216261ce..f914c03c 100644 --- a/pysite/views/api/bot/clean.py +++ b/pysite/views/api/bot/clean.py @@ -11,7 +11,8 @@ POST_SCHEMA = Schema({ { "author": str, "content": str, - "timestamp": str + "timestamp": str, + "role": str } ] }) diff --git a/pysite/views/main/bot/cleanlog.py b/pysite/views/main/bot/cleanlog.py index abd1ee2e..1c90dc0b 100644 --- a/pysite/views/main/bot/cleanlog.py +++ b/pysite/views/main/bot/cleanlog.py @@ -1,8 +1,7 @@ import logging -import random from pysite.base_route import RouteView -from pysite.constants import ALL_STAFF_ROLES +from pysite.constants import ALL_STAFF_ROLES, ROLE_COLORS from pysite.decorators import require_roles from pysite.mixins import DBMixin, OAuthMixin @@ -16,35 +15,6 @@ class CleanLogView(RouteView, DBMixin, OAuthMixin): table_name = "clean_logs" template = "main/bot/clean_logs.html" - # Colors - author_colors = {} - all_colors = [ - "#afcfff", - "#93ff91", - "#ffe559", - "#ff9036", - "#ff6c5e", - "#ff65be", - "#9298ff", - ] - color_pool = all_colors - - def _assign_color(self, author): - """ - Assign a color to a specific author. - """ - - if not self.color_pool: - self.color_pool = self.all_colors - - if author not in self.author_colors: - random_index = random.randint(0, len(self.color_pool)) - color = self.color_pool.pop(random_index) - self.author_colors[author] = color - return color - else: - return self.author_colors[author] - @require_roles(ALL_STAFF_ROLES) def get(self, log_id): """ @@ -60,6 +30,6 @@ class CleanLogView(RouteView, DBMixin, OAuthMixin): messages = data["log_data"] for message in messages: - message['color'] = self._assign_color(message['author']) + message['color'] = ROLE_COLORS.get(message['role'], ROLE_COLORS['developers']) return self.render(self.template, messages=messages) diff --git a/tests/test_clean_logs.py b/tests/test_clean_logs.py index 210306c0..4932b4b6 100644 --- a/tests/test_clean_logs.py +++ b/tests/test_clean_logs.py @@ -29,8 +29,9 @@ class TestCleanLogAPI(SiteTest): good_data = json.dumps({ "log_data": [ { - "author": "something", - "content": "testy", + "author": "something", + "role": "awful", + "content": "testy", "timestamp": "this way comes" } ] -- cgit v1.2.3 From 2844cee97010b881e05598d5a62520cdc5f37dee Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 29 Jul 2018 01:30:51 +0200 Subject: Fixed broken test --- tests/test_clean_logs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_clean_logs.py b/tests/test_clean_logs.py index 4932b4b6..886a4c3a 100644 --- a/tests/test_clean_logs.py +++ b/tests/test_clean_logs.py @@ -30,9 +30,12 @@ class TestCleanLogAPI(SiteTest): "log_data": [ { "author": "something", + "user_id": "12345151", "role": "awful", "content": "testy", - "timestamp": "this way comes" + "timestamp": "this way comes", + "embeds": [{"fire":"nanny"}], + "attachments": [""], } ] }) -- cgit v1.2.3 From b7f3cc086bdd27e7fb8a656ad66348b6bf489268 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 29 Jul 2018 11:41:28 +0200 Subject: Addressing gdude comments --- pysite/constants.py | 60 +++++++++++++++++++++++---------------- pysite/views/api/bot/clean.py | 2 +- pysite/views/main/bot/cleanlog.py | 4 +-- tests/test_clean_logs.py | 4 +-- 4 files changed, 40 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/pysite/constants.py b/pysite/constants.py index d7453871..7d8dbf6e 100644 --- a/pysite/constants.py +++ b/pysite/constants.py @@ -31,17 +31,27 @@ class BotEventTypes(Enum): DEBUG_MODE = "FLASK_DEBUG" in environ # All snowflakes should be strings as RethinkDB rounds them as ints -OWNER_ROLE = "267627879762755584" -ADMIN_ROLE = "267628507062992896" -MODERATOR_ROLE = "267629731250176001" +ADMIN_BOTS_ROLE = "270988689419665409" +ADMINS_ROLE = "267628507062992896" +ANNOUNCEMENTS_ROLE = "463658397560995840" +BOTS_ROLE = "277546923144249364" +CODE_JAM_CHAMPIONS_ROLE = "430492892331769857" +CONTRIBS_ROLE = "295488872404484098" DEVOPS_ROLE = "409416496733880320" -HELPER_ROLE = "267630620367257601" -CONTRIB_ROLE = "295488872404484098" +DEVELOPERS_ROLE = "352427296948486144" +HELPERS_ROLE = "267630620367257601" JAMMERS_ROLE = "423054537079783434" - -ALL_STAFF_ROLES = (OWNER_ROLE, ADMIN_ROLE, MODERATOR_ROLE, DEVOPS_ROLE) -TABLE_MANAGER_ROLES = (OWNER_ROLE, ADMIN_ROLE, DEVOPS_ROLE) -EDITOR_ROLES = ALL_STAFF_ROLES + (HELPER_ROLE, CONTRIB_ROLE) +MODERATORS_ROLE = "267629731250176001" +MUTED_ROLE = "277914926603829249" +OWNERS_ROLE = "267627879762755584" +PARTNERS_ROLE = "323426753857191936" +PYTHON_ROLE = "458226699344019457" +STREAMERS_ROLE = "462650825978806274" +SUBREDDIT_MOD_ROLE = "458226413825294336" + +ALL_STAFF_ROLES = (OWNERS_ROLE, ADMINS_ROLE, MODERATORS_ROLE, DEVOPS_ROLE) +TABLE_MANAGER_ROLES = (OWNERS_ROLE, ADMINS_ROLE, DEVOPS_ROLE) +EDITOR_ROLES = ALL_STAFF_ROLES + (HELPERS_ROLE, CONTRIBS_ROLE) SERVER_ID = 267624335836053506 @@ -103,22 +113,22 @@ JAM_QUESTION_TYPES = [ # Server role colors ROLE_COLORS = { - "owners": "#ffa3a1", - "admins": "#e76e6c", - "moderators": "#ce3c42", - "python": "#6f9fed", - "bots": "#6f9fed", - "admin bots": "#6f9fed", - "devops": "#a1d1ff", - "helpers": "#e0b000", - "subreddit moderators": "#d897ed", - "partners": "#b66fed", - "streamers": "#833cba", - "developers": "#fcfcfc", - "muted": "#fcfcfc", - "code jam champions": "#b108b4", - "jammers": "#258639", - "contributors": "#55cc6c" + ADMIN_BOTS_ROLE: "#6f9fed", + ADMINS_ROLE: "#e76e6c", + BOTS_ROLE: "#6f9fed", + CODE_JAM_CHAMPIONS_ROLE: "#b108b4", + CONTRIBS_ROLE: "#55cc6c", + DEVOPS_ROLE: "#a1d1ff", + DEVELOPERS_ROLE: "#fcfcfc", + HELPERS_ROLE: "#e0b000", + JAMMERS_ROLE: "#258639", + MODERATORS_ROLE: "#ce3c42", + MUTED_ROLE: "#fcfcfc", + OWNERS_ROLE: "#ffa3a1", + PARTNERS_ROLE: "#b66fed", + PYTHON_ROLE: "#6f9fed", + STREAMERS_ROLE: "#833cba", + SUBREDDIT_MOD_ROLE: "#d897ed", } # CSRF diff --git a/pysite/views/api/bot/clean.py b/pysite/views/api/bot/clean.py index 2ee3f62d..82d1e735 100644 --- a/pysite/views/api/bot/clean.py +++ b/pysite/views/api/bot/clean.py @@ -12,7 +12,7 @@ POST_SCHEMA = Schema({ "author": str, "user_id": str, "content": str, - "role": str, + "role_id": str, "timestamp": str, "embeds": object, "attachments": [str], diff --git a/pysite/views/main/bot/cleanlog.py b/pysite/views/main/bot/cleanlog.py index 1c90dc0b..9c719b3e 100644 --- a/pysite/views/main/bot/cleanlog.py +++ b/pysite/views/main/bot/cleanlog.py @@ -1,7 +1,7 @@ import logging from pysite.base_route import RouteView -from pysite.constants import ALL_STAFF_ROLES, ROLE_COLORS +from pysite.constants import ALL_STAFF_ROLES, DEVELOPERS_ROLE, ROLE_COLORS from pysite.decorators import require_roles from pysite.mixins import DBMixin, OAuthMixin @@ -30,6 +30,6 @@ class CleanLogView(RouteView, DBMixin, OAuthMixin): messages = data["log_data"] for message in messages: - message['color'] = ROLE_COLORS.get(message['role'], ROLE_COLORS['developers']) + message['color'] = ROLE_COLORS.get(message['role_id'], ROLE_COLORS[DEVELOPERS_ROLE]) return self.render(self.template, messages=messages) diff --git a/tests/test_clean_logs.py b/tests/test_clean_logs.py index 886a4c3a..1c3449b4 100644 --- a/tests/test_clean_logs.py +++ b/tests/test_clean_logs.py @@ -9,7 +9,7 @@ class TestCleanLogAPI(SiteTest): Tests submitting a clean log and verifies that we get a UUID in return. - Then tests that + Also ensures that we get a 400 if we send in bad data. """ def test_returns_400_on_bad_data(self): @@ -31,7 +31,7 @@ class TestCleanLogAPI(SiteTest): { "author": "something", "user_id": "12345151", - "role": "awful", + "role_id": "4818413", "content": "testy", "timestamp": "this way comes", "embeds": [{"fire":"nanny"}], -- cgit v1.2.3