Skip to content

Commit

Permalink
graphql: Small changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurocon committed Dec 23, 2024
1 parent 908852a commit a75777a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
8 changes: 4 additions & 4 deletions amelie/graphql/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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})!"
)

Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions amelie/graphql/tests/test_activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions amelie/graphql/tests/test_companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
12 changes: 4 additions & 8 deletions amelie/graphql/tests/test_education.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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(
Expand Down
18 changes: 9 additions & 9 deletions amelie/graphql/tests/test_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions amelie/graphql/tests/test_videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion amelie/settings/local.py.default
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion amelie/settings/local.py.localdev
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a75777a

Please sign in to comment.