Skip to content

Commit

Permalink
fix: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martinalbert committed Jun 17, 2024
1 parent f236283 commit e98745a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 68 deletions.
12 changes: 3 additions & 9 deletions butter_cms/tests/test_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.author import Author
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListAuthor(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListAuthor(TestCaseWithAPIKey):
@vcr.use_cassette('authors/test_author_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -127,10 +124,7 @@ def test_all_include_recent_posts(self):
}
)

class TestRetrieveAuthor(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrieveAuthor(TestCaseWithAPIKey):
@vcr.use_cassette('authors/test_author_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
12 changes: 3 additions & 9 deletions butter_cms/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.category import Category
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListCategory(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListCategory(TestCaseWithAPIKey):
@vcr.use_cassette('categories/test_category_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -107,10 +104,7 @@ def test_all_include_recent_posts(self):
}
)

class TestRetrieveCategory(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrieveCategory(TestCaseWithAPIKey):
@vcr.use_cassette('categories/test_category_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
12 changes: 3 additions & 9 deletions butter_cms/tests/test_content_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.content_field import ContentField
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListContentField(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListContentField(TestCaseWithAPIKey):
@vcr.use_cassette('content_fields/test_content_field_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -100,10 +97,7 @@ def test_list_non_existent_content_field(self):
self.assertIn('detail', response)
self.assertEqual('Content key non-existent-key not found', response['detail'])

class TestRetrieveContentField(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrieveContentField(TestCaseWithAPIKey):
@vcr.use_cassette('content_fields/test_content_field_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
7 changes: 2 additions & 5 deletions butter_cms/tests/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.feed import Feed
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestFeed(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestFeed(TestCaseWithAPIKey):
@vcr.use_cassette('feeds/test_feed_sitemap_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
17 changes: 4 additions & 13 deletions butter_cms/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.page import Page
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListPages(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListPages(TestCaseWithAPIKey):
@vcr.use_cassette('pages/test_page_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -177,10 +174,7 @@ def test_list_pages_without_page_type(self):
}
)

class TestRetrievePage(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrievePage(TestCaseWithAPIKey):
@vcr.use_cassette('pages/test_page_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -316,10 +310,7 @@ def test_get_non_existent_page(self):
self.assertIn('detail', response)
self.assertEqual('Not found', response['detail'])

class TestSearchPage(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestSearchPage(TestCaseWithAPIKey):
@vcr.use_cassette('pages/test_page_search_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
17 changes: 4 additions & 13 deletions butter_cms/tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.post import Post
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListPosts(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListPosts(TestCaseWithAPIKey):
@vcr.use_cassette('posts/test_post_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -133,10 +130,7 @@ def test_list_posts_without_body(self):
}
)

class TestRetrievePost(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrievePost(TestCaseWithAPIKey):
@vcr.use_cassette('posts/test_post_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -210,10 +204,7 @@ def test_get_non_existent_post(self):
self.assertIn('detail', response)
self.assertEqual('Not found', response['detail'])

class TestSearchPost(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestSearchPost(TestCaseWithAPIKey):
@vcr.use_cassette('posts/test_post_search_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
12 changes: 3 additions & 9 deletions butter_cms/tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import os
from butter_cms.tag import Tag
from requests.exceptions import HTTPError
from test_vcr_config import vcr_setup
from test_vcr_config import TestCaseWithAPIKey, vcr_setup

vcr = vcr_setup()

class TestListTags(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestListTags(TestCaseWithAPIKey):
@vcr.use_cassette('tags/test_tag_all_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down Expand Up @@ -106,10 +103,7 @@ def test_list_tags_with_recent_posts(self):
}
)

class TestRetrieveTag(unittest.TestCase):
def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

class TestRetrieveTag(TestCaseWithAPIKey):
@vcr.use_cassette('tags/test_tag_get_invalid_token.json')
def test_invalid_token(self):
with self.assertRaises(HTTPError):
Expand Down
26 changes: 25 additions & 1 deletion butter_cms/tests/test_vcr_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import os
import socket
import unittest

# Block network access
# Comment out the following code block if you want to run the tests with real network access
def guard(*args, **kwargs):
raise Exception("Blocked network access. Use VCR cassettes instead.")
socket.socket = guard
Expand Down Expand Up @@ -39,4 +44,23 @@ def vcr_setup():
decode_compressed_response=True,
before_record_response=response_filter(),
serializer='json',
)
)

class TestCaseWithAPIKey(unittest.TestCase):
"""Unit Test Case with API key
This class is used to test the ButterCMS API with VCR
The VCR library is used to record the API response and replay it in the future.
There are multiple modes like 'once', 'new_episodes', 'none' and 'all'.
More information about each of them can be found here: https://vcrpy.readthedocs.io/en/latest/usage.html#record-modes
For VCR recordings or cassetes to work, at least 'once' the test should be run with real network access.
This is to record the API response and save it in the cassette for future unit tests.
To re-record the API response - tying the API key to the cassette.
You can use record_mode='new_episodes' to record the API responses again.
You can also delete the cassette file to re-record the missing API response with default mode 'once'.
"""

def setUp(self):
self.token = os.getenv('DEMO_TOKEN')

0 comments on commit e98745a

Please sign in to comment.