forked from mtusi2026/mtusi2026lab2
PostgreSQL с версионируемыми миграциями, вход по Basic и Bearer-сессии в Valkey (AOF), Argon2id, compose-стенд с секретами в файлах. Собственные тесты в student-tests/, проверки инфраструктуры и EXPLAIN в scripts/, отчёт в REPORT.md, результаты в evidence/, команды для показа в DEMO.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
4.0 KiB
Python
85 lines
4.0 KiB
Python
"""Доступ к чатам: аутентификация (кто ты) отдельно от доступа к конкретному чату (можно ли тебе сюда)."""
|
|
import unittest
|
|
import uuid
|
|
|
|
import api
|
|
|
|
|
|
class AuthenticationOrderTest(unittest.TestCase):
|
|
def test_authentication_is_checked_before_body_and_resource(self):
|
|
self.assertEqual(api.request('POST', '/api/v1/chats', raw=b'{broken').status, 401)
|
|
owner = api.new_user()
|
|
chat = api.new_chat(owner, [owner])
|
|
path = '/api/v1/chats/%s/messages' % chat['id']
|
|
self.assertEqual(api.request('GET', path).status, 401)
|
|
self.assertEqual(api.request('POST', path, raw=b'{broken').status, 401)
|
|
|
|
def test_registration_does_not_need_a_token(self):
|
|
response, _ = api.register()
|
|
self.assertEqual(response.status, 201)
|
|
|
|
|
|
class ForeignChatTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.alice, cls.bob, cls.eve = api.new_user('Алиса'), api.new_user('Боб'), api.new_user('Ева')
|
|
cls.chat = api.new_chat(cls.alice, [cls.alice, cls.bob])
|
|
cls.path = '/api/v1/chats/' + cls.chat['id']
|
|
assert api.send(cls.alice, cls.chat, 'секрет участников').status == 201
|
|
|
|
def assert_not_found(self, response):
|
|
self.assertEqual(response.status, 404)
|
|
self.assertEqual(response.json['error']['code'], 'not_found')
|
|
|
|
def test_outsider_cannot_get_chat(self):
|
|
self.assert_not_found(api.request('GET', self.path, user=self.eve))
|
|
|
|
def test_outsider_cannot_read_history(self):
|
|
self.assert_not_found(api.request('GET', self.path + '/messages', user=self.eve))
|
|
|
|
def test_outsider_cannot_write(self):
|
|
self.assert_not_found(api.send(self.eve, self.chat, 'чужое сообщение'))
|
|
self.assert_not_found(api.request('POST', self.path + '/messages', user=self.eve, body={'text': ''}))
|
|
history = [m for page in api.pages(self.path + '/messages', self.alice, 100) for m in page]
|
|
self.assertNotIn('чужое сообщение', [m['text'] for m in history])
|
|
self.assertNotIn(self.eve['id'], [m['sender_id'] for m in history])
|
|
|
|
def test_foreign_chat_looks_like_missing_chat(self):
|
|
foreign = api.request('GET', self.path, user=self.eve)
|
|
missing = api.request('GET', '/api/v1/chats/' + str(uuid.uuid4()), user=self.eve)
|
|
self.assert_not_found(missing)
|
|
self.assertEqual(foreign.json['error']['code'], missing.json['error']['code'])
|
|
self.assertEqual(foreign.json['error']['message'], missing.json['error']['message'])
|
|
|
|
def test_chat_list_contains_only_own_chats(self):
|
|
def chat_ids(user):
|
|
return [chat['id'] for page in api.pages('/api/v1/chats', user, 100) for chat in page]
|
|
|
|
self.assertIn(self.chat['id'], chat_ids(self.alice))
|
|
self.assertIn(self.chat['id'], chat_ids(self.bob))
|
|
self.assertEqual(chat_ids(self.eve), [])
|
|
|
|
def test_every_member_can_read_and_write(self):
|
|
self.assertEqual(api.request('GET', self.path, user=self.bob).json, self.chat)
|
|
sent = api.send(self.bob, self.chat, 'ответ Боба')
|
|
self.assertEqual(sent.status, 201)
|
|
self.assertEqual(sent.json['sender_id'], self.bob['id'])
|
|
|
|
def test_x_user_id_does_not_open_a_foreign_chat(self):
|
|
response = api.request('GET', self.path, user=self.eve, headers={'X-User-Id': self.alice['id']})
|
|
self.assert_not_found(response)
|
|
|
|
def test_invalid_chat_id_is_400(self):
|
|
for suffix in ('', '/messages'):
|
|
with self.subTest(suffix=suffix):
|
|
response = api.request('GET', '/api/v1/chats/not-a-uuid' + suffix, user=self.alice)
|
|
self.assertEqual(response.status, 400)
|
|
|
|
def test_unknown_route_is_404(self):
|
|
self.assert_not_found(api.request('GET', '/api/v1/unknown', user=self.alice))
|
|
self.assert_not_found(api.request('DELETE', self.path, user=self.alice))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|