toolshed/backend/toolshed/tests/test_api.py
2026-09-01 02:12:59 +02:00

74 lines
3.6 KiB
Python

from django.test import Client
from authentication.tests import UserTestMixin, SignatureAuthClient, ToolshedTestCase
from backend import settings
from toolshed.tests import CategoryTestMixin, TagTestMixin, PropertyTestMixin
anonymous_client = Client()
client = SignatureAuthClient()
class CombinedApiTestCase(UserTestMixin, CategoryTestMixin, TagTestMixin, PropertyTestMixin, ToolshedTestCase):
def setUp(self):
super().setUp()
self.prepare_users()
self.prepare_categories()
self.prepare_tags()
self.prepare_properties()
def test_version_anonymous(self):
response = anonymous_client.get('/api/version/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'version': settings.TOOLSHED_VERSION, 'commit': settings.GIT_COMMIT})
def test_version_authenticated(self):
response = client.get('/api/version/', self.f['local_user1'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'version': settings.TOOLSHED_VERSION, 'commit': settings.GIT_COMMIT})
def test_domains_anonymous(self):
response = anonymous_client.get('/api/v1/domains/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), ['example.com'])
def test_domains_authenticated(self):
response = client.get('/api/v1/domains/', self.f['local_user1'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), ['example.com'])
def test_policy_api_anonymous(self):
response = anonymous_client.get('/api/v1/availability_policies/')
self.assertEqual(response.status_code, 403)
def test_policy_api(self):
response = client.get('/api/v1/availability_policies/', self.f['local_user1'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), [['sell', 'Sell'], ['rent', 'Rent'], ['lend', 'Lend'], ['share', 'Share'],
['private', 'Private']])
def test_visibility_policy_api_anonymous(self):
response = anonymous_client.get('/api/v1/visibility_policies/')
self.assertEqual(response.status_code, 403)
def test_visibility_policy_api(self):
response = client.get('/api/v1/visibility_policies/', self.f['local_user1'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), [['public', 'Public'], ['friends', 'Friends'], ['private', 'Private']])
def test_combined_api_anonymous(self):
response = anonymous_client.get('/api/v1/info/')
self.assertEqual(response.status_code, 403)
def test_combined_api(self):
response = client.get('/api/v1/info/', self.f['local_user1'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['availability_policies'], [['sell', 'Sell'], ['rent', 'Rent'], ['lend', 'Lend'],
['share', 'Share'], ['private', 'Private']])
self.assertEqual(response.json()['visibility_policies'],
[['public', 'Public'], ['friends', 'Friends'], ['private', 'Private']])
self.assertEqual(response.json()['categories'],
['cat1', 'cat2', 'cat3', 'cat1/subcat1', 'cat1/subcat2', 'cat1/subcat1/subcat1',
'cat1/subcat1/subcat2'])
self.assertEqual(response.json()['tags'], ['tag1', 'tag2', 'tag3'])
self.assertEqual([p['name'] for p in response.json()['properties']], ['prop1', 'prop2', 'prop3'])
self.assertEqual(response.json()['domains'], ['example.com'])