From a75777a243326f8a840a57a779452492a13d8769 Mon Sep 17 00:00:00 2001 From: Kevin Alberts Date: Mon, 23 Dec 2024 13:10:29 +0100 Subject: [PATCH] graphql: Small changes after review --- amelie/graphql/tests/__init__.py | 8 ++++---- amelie/graphql/tests/test_activities.py | 8 ++++---- amelie/graphql/tests/test_companies.py | 4 ++-- amelie/graphql/tests/test_education.py | 12 ++++-------- amelie/graphql/tests/test_news.py | 18 +++++++++--------- amelie/graphql/tests/test_videos.py | 4 ++-- amelie/settings/local.py.default | 5 ++++- amelie/settings/local.py.localdev | 5 ++++- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/amelie/graphql/tests/__init__.py b/amelie/graphql/tests/__init__.py index f732e41..3a799b8 100644 --- a/amelie/graphql/tests/__init__.py +++ b/amelie/graphql/tests/__init__.py @@ -76,7 +76,7 @@ def _test_private_model(self, query_name: str, public_field_spec: str = "id", ) # Check if the error received has the correct error message format, so we don't # accidentally consider a different error as a 'successful' result. - content = json.loads(response.content) + content = response.json() for error in content['errors']: # If different error than expected, throw error. self.assertRegex( @@ -132,10 +132,10 @@ def _test_private_model_list(self, query_name: str, public_field_spec: str = "id ) # Check if the results list is empty. - content = json.loads(response.content) + content = response.json() num_obj = len(content['data'][query_name]['results']) self.assertEqual( - content['data'][query_name]['results'], [], + num_obj, 0, f"Query for '{query_name}', private object '{query_variables}' did not return 0 expected objects (returned {num_obj})!" ) @@ -180,7 +180,7 @@ def _test_public_model_and_private_field(self, query_name: str, field_name: str, ) # Check if the error received has the correct error message format, so we don't # accidentally consider a different error as a 'successful' result. - content = json.loads(response.content) + content = response.json() for error in content['errors']: # If different error than expected, throw error. self.assertRegex( diff --git a/amelie/graphql/tests/test_activities.py b/amelie/graphql/tests/test_activities.py index c4ec594..949cb04 100644 --- a/amelie/graphql/tests/test_activities.py +++ b/amelie/graphql/tests/test_activities.py @@ -98,7 +98,7 @@ def test_activity_private_attachment(self): # Test if private activity attachments are hidden in get view query = "query ($id: ID) { activity(id: $id) { attachments { public }}}" response = self.query(query, variables={"id": self.public_activity.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -119,7 +119,7 @@ def test_activities_private_attachment(self): # Test if private activity attachments are hidden in list view query = "query ($id: ID) { activities(id: $id) { results { attachments { public }}}}" response = self.query(query, variables={"id": self.public_activity.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -140,7 +140,7 @@ def test_activity_private_photo(self): # Test if private activity photos are hidden in get view query = "query ($id: ID) { activity(id: $id) { photos { public }}}" response = self.query(query, variables={"id": self.public_activity.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -161,7 +161,7 @@ def test_activities_private_photo(self): # Test if private activity photos are hidden in list view query = "query ($id: ID) { activities(id: $id) { results { photos { public }}}}" response = self.query(query, variables={"id": self.public_activity.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( diff --git a/amelie/graphql/tests/test_companies.py b/amelie/graphql/tests/test_companies.py index 9ff56f7..911a9b8 100644 --- a/amelie/graphql/tests/test_companies.py +++ b/amelie/graphql/tests/test_companies.py @@ -163,7 +163,7 @@ def test_company_event_private_attachment(self): # Test if private event attachments are hidden in get view query = "query ($id: ID) { companyEvent(id: $id) { attachments { public }}}" response = self.query(query, variables={"id": self.public_visible_event.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -184,7 +184,7 @@ def test_company_events_private_attachment(self): # Test if private event attachments are hidden in list view query = "query ($id: ID) { companyEvents(id: $id) { results { attachments { public }}}}" response = self.query(query, variables={"id": self.public_visible_event.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( diff --git a/amelie/graphql/tests/test_education.py b/amelie/graphql/tests/test_education.py index d687073..84eb96b 100644 --- a/amelie/graphql/tests/test_education.py +++ b/amelie/graphql/tests/test_education.py @@ -16,11 +16,7 @@ def generate_education_events(): """ Generate Education Events for testing. - It will generate 4 events: - - A public event that is visible - - A public event that is not visible - - A private event that is visible - - A private event that is not visible + It will generate a public and a private event. """ now = timezone.now() @@ -126,7 +122,7 @@ def test_education_event_private_attachment(self): # Test if private event attachments are hidden in get view query = "query ($id: ID) { educationEvent(id: $id) { attachments { public }}}" response = self.query(query, variables={"id": self.public_event.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -136,7 +132,7 @@ def test_education_event_private_attachment(self): # Check that all attachments are public, and that the correct amount of attachments are received (1) self.assertTrue(all(a['public'] == True for a in content['data']['educationEvent']['attachments']), - f"Query for 'educationEvent', public field 'attachments' returned a private attachment!") + "Query for 'educationEvent', public field 'attachments' returned a private attachment!") num_attachments = len(content['data']['educationEvent']['attachments']) self.assertEqual( num_attachments, 1, @@ -147,7 +143,7 @@ def test_education_events_private_attachment(self): # Test if private event attachments are hidden in list view query = "query ($id: ID) { educationEvents(id: $id) { results { attachments { public }}}}" response = self.query(query, variables={"id": self.public_event.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( diff --git a/amelie/graphql/tests/test_news.py b/amelie/graphql/tests/test_news.py index 73a4027..f0fc81c 100644 --- a/amelie/graphql/tests/test_news.py +++ b/amelie/graphql/tests/test_news.py @@ -75,9 +75,9 @@ class NewsGraphQLPrivateFieldTests(BaseGraphQLPrivateFieldTests): def setUp(self): super(NewsGraphQLPrivateFieldTests, self).setUp() - # Create required committees for the news module - educom = Committee(name="EduCom", abbreviation=settings.EDUCATION_COMMITTEE_ABBR) - educom.save() + # A committee with the abbreviation of the education committee is required for the news module + # functions to work properly. So we get_or_create it here to make sure it exists in the test DB. + _ = Committee.objects.get_or_create(name="EduCom", abbreviation=settings.EDUCATION_COMMITTEE_ABBR) # Generate news article self.article = generate_news_article() @@ -86,7 +86,7 @@ def test_news_item_private_attachment(self): # Test if private attachments are hidden in get view query = "query ($id: ID) { newsItem(id: $id) { attachments { public }}}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -107,7 +107,7 @@ def test_news_items_private_attachment(self): # Test if private attachments are hidden in list view query = "query ($id: ID) { newsItems(id: $id) { results { attachments { public }}}}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -128,7 +128,7 @@ def test_news_item_private_activity(self): # Test if private activities are hidden in get view query = "query ($id: ID) { newsItem(id: $id) { activities { public }}}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -149,7 +149,7 @@ def test_news_items_private_activity(self): # Test if private activities are hidden in list view query = "query ($id: ID) { newsItems(id: $id) { results { activities { public }}}}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -170,7 +170,7 @@ def test_news_item_author_publisher_string(self): # Test if the author and publisher of a news item are a strings in get view query = "query ($id: ID) { newsItem(id: $id) { author, publisher }}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -188,7 +188,7 @@ def test_news_items_author_publisher_string(self): # Test if the author and publisher of a news item are a strings in list view query = "query ($id: ID) { newsItems(id: $id) { results { author, publisher }}}" response = self.query(query, variables={"id": self.article.id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( diff --git a/amelie/graphql/tests/test_videos.py b/amelie/graphql/tests/test_videos.py index 14d727b..f4b72d6 100644 --- a/amelie/graphql/tests/test_videos.py +++ b/amelie/graphql/tests/test_videos.py @@ -97,7 +97,7 @@ def test_video_publisher_string(self): # Test if the publisher of a video is a string in get view query = "query ($videoId: ID) { video(videoId: $videoId) { publisher }}" response = self.query(query, variables={"videoId": self.public_yt_video.video_id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( @@ -113,7 +113,7 @@ def test_videos_publisher_string(self): # Test if the publisher of a video is a string in list view query = "query ($videoId: ID) { videos(videoId: $videoId) { results { publisher }}}" response = self.query(query, variables={"videoId": self.public_yt_video.video_id}) - content = json.loads(response.content) + content = response.json() # The request should succeed self.assertResponseNoErrors( diff --git a/amelie/settings/local.py.default b/amelie/settings/local.py.default index c2d82b6..62c9d65 100644 --- a/amelie/settings/local.py.default +++ b/amelie/settings/local.py.default @@ -31,9 +31,12 @@ DATABASES = { # Definitely DON'T put these settings in production! ENV = 'DEBUG' DEBUG = True -DEBUG_TOOLBAR = True and "test" not in sys.argv +DEBUG_TOOLBAR = True ALLOWED_HOSTS = ["*"] +# Debug toolbar does not work during tests, so to make tests work, we exclude the toolbar if we're testing +DEBUG_TOOLBAR = DEBUG_TOOLBAR and "test" not in sys.argv + # Default setting for enabling profiling (the rest of the config is done in local.py(.default) # If this is turned on a lot of data will be generated and stored in the database. So only turn it on if you feel bold. ENABLE_REQUEST_PROFILING = False diff --git a/amelie/settings/local.py.localdev b/amelie/settings/local.py.localdev index a6c9673..267200c 100644 --- a/amelie/settings/local.py.localdev +++ b/amelie/settings/local.py.localdev @@ -23,9 +23,12 @@ DATABASES = { # Definitely DON'T put these settings in production! ENV = 'DEBUG' DEBUG = True -DEBUG_TOOLBAR = True and "test" not in sys.argv +DEBUG_TOOLBAR = True ALLOWED_HOSTS = ["*"] +# Debug toolbar does not work during tests, so to make tests work, we exclude the toolbar if we're testing +DEBUG_TOOLBAR = DEBUG_TOOLBAR and "test" not in sys.argv + # Disable secure redirects to allow local development without SSL SECURE_SSL_REDIRECT = False