async def setUp(self): super().setUp() self.user1 = User.objects.create_user( email='*****@*****.**', password='******', username='******', ) self.user2 = User.objects.create_user( email='*****@*****.**', password='******', username='******', ) self.party = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user1, ) self.party.save() self.state = self.party.get_state() self.client.login(email=self.user1.email, password='******') client2 = Client() client2.login(email=self.user2.email, password='******') self.communicator1 = new_communicator(self.user1) self.communicator2 = new_communicator(self.user2) await self.communicator1.connect() await self.communicator2.connect() await self.communicator1.receive_json_from(1) await self.communicator2.receive_json_from(1)
class TestCaseWithSingleWebsocket(TestCaseWithCache): async def join(self): await self.communicator.send_json_to({ 'command': 'party.join', 'party_id': self.party.id, }) await self.communicator.receive_json_from(1) @async_test async def setUp(self): super().setUp() self.user = User.objects.create_user( email='*****@*****.**', password='******', username='******', ) self.party = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user, ) self.party.save() self.state = self.party.get_state() self.client.login(email=self.user.email, password='******') self.communicator = new_communicator(self.user) await self.communicator.connect() await self.communicator.receive_json_from(1) @async_test async def tearDown(self): await self.communicator.disconnect() super().tearDown()
def setUp(self): self.user1 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.user2 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.user3 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.party = Party(name="rust party", location="rustland", type=int(PartyType.InGroup), leader=self.user1) self.party.save() self.state = self.party.get_state() self.restaurant = Restaurant(name="Rustaurant") self.restaurant.save() self.menu1 = Menu(name="Trait", price=10000) self.menu1.save() self.menu2 = Menu(name="Struct", price=33000) self.menu2.save() self.menu3 = Menu(name="Macro", price=13130) self.menu3.save() self.state.member_ids = [self.user1.id, self.user2.id, self.user3.id] self.state.member_ids_backup = [ self.user1.id, self.user2.id, self.user3.id ] self.state.menu_entries.add( self.menu1.id, 3, [self.user1.id, self.user2.id, self.user3.id]) self.state.menu_entries.add( self.menu2.id, 1, [self.user1.id, self.user2.id, self.user3.id]) self.state.menu_entries.add( self.menu3.id, 6, [self.user1.id, self.user2.id, self.user3.id]) self.state.phase = PartyPhase.PaymentAndCollection self.state.paid_user_id = self.user2.id self.state.restaurant_id = self.restaurant.id self.state.save() self.party.restaurant_id = self.restaurant.id self.party.save()
def setUp(self): super().setUp() self.user = User.objects.create_user( email='*****@*****.**', password='******', username='******', ) self.party = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user, ) self.party.save() self.state = self.party.get_state() cache.set('user-party:{}'.format(self.user.id), self.party.id) self.client.login(email=self.user.email, password='******')
def get_user_parties(request): try: token = request.META["HTTP_AUTHORIZATION"] received_json_data = json.loads(request.body) user = User.objects.get(spotify_id=received_json_data['userId']) except ValueError: return HttpResponse("Inavlid Json", status=400) if not user.is_authenticated(token): return HttpResponse("No authenticated", status=403) parties = Party.get_parties_with(user) serializer = PartySecretSerializer(parties, many=True) return JSONResponse(serializer.data, status=200)
def setUp(self): super().setUp() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.party1 = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user, ) self.party1.save() self.state1 = self.party1.get_state() self.party2 = Party( name="party 2 name", type=int(PartyType.InGroup), location="party 2 location", leader=self.user, ) self.party2.save() self.state2 = self.party2.get_state()
def test_delete_party_with_invalid_state(self): party = Party( name="new party name", type=int(PartyType.Private), location="new party location", leader=self.user, ) party.save() id = party.id self.login() party.get_state().delete() resp = self.delete('/api/party/{}/'.format(id)) self.assertEqual(resp.status_code, 200)
def create_party(request): try: token = request.META["HTTP_AUTHENTICATION"] received_json_data = json.loads(request.body) owner = User.objects.get(spotifyId=received_json_data['userId']) party_name = received_json_data['partyName'] except ValueError: return HttpResponse("Inavlid Json", status=400) if owner.is_authenticated(token): return HttpResponse("No authenticated", status=403) party = Party.create_party(owner, party_name) party.save() party.members.add(owner) party.save() serializer = PartySerializer(party, many=False) return JSONResponse(serializer.data, status=201)
def create_party(request): try: token = request.META["HTTP_AUTHORIZATION"] received_json_data = json.loads(request.body) owner = User.objects.get(spotify_id=received_json_data['userId']) party_name = received_json_data['partyName'] except ValueError: return HttpResponse("Inavlid Json", status=400) except KeyError: return HttpResponse("Inavlid Json", status=400) if not owner.is_authenticated(token): return HttpResponse("No authenticated", status=403) party = Party.create_party(owner, party_name) party.save() party.members.add(owner) party.save() serializer = PartySecretSerializer(party, many=False) return JSONResponse(serializer.data, status=201)
class PartyTestCase(TestCaseWithHttp): def setUp(self): super().setUp() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.party1 = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user, ) self.party1.save() self.state1 = self.party1.get_state() self.party2 = Party( name="party 2 name", type=int(PartyType.InGroup), location="party 2 location", leader=self.user, ) self.party2.save() self.state2 = self.party2.get_state() def login(self): super().login(email='*****@*****.**', password='******') def test_not_allowed(self): self.assertEqual(self.put('/api/party/', {}).status_code, 405) self.assertEqual(self.delete('/api/party/').status_code, 405) self.assertEqual(self.post('/api/party/0/', {}).status_code, 405) self.assertEqual(self.put('/api/party/0/', {}).status_code, 405) def test_unauthenticated(self): self.assertEqual(self.get('/api/party/').status_code, 403) self.assertEqual(self.post('/api/party/', {}).status_code, 403) self.assertEqual(self.get('/api/party/0/').status_code, 403) self.assertEqual(self.delete('/api/party/0/').status_code, 403) self.login() self.assertEqual(self.get('/api/party/').status_code, 200) self.logout() self.assertEqual(self.get('/api/party/').status_code, 403) def test_not_found(self): self.login() self.assertEqual(self.get('/api/party/0/').status_code, 404) self.assertEqual(self.delete('/api/party/0/').status_code, 404) def test_get_party_state(self): state1 = self.state1 self.assertIsNotNone(state1) state2 = self.state2 self.assertIsNotNone(state2) def test_get_party(self): self.login() resp = self.get('/api/party/') self.assertEqual(resp.status_code, 200) resp_json = resp.json() self.assertEqual(len(resp_json), 2) self.assertListEqual(resp_json, [self.party1.as_dict(), self.party2.as_dict()]) def test_post_party(self): self.login() new_party = { 'name': 'new party name', 'type': PartyType.Private, 'location': 'new party location', } resp = self.post('/api/party/', new_party) self.assertEqual(resp.status_code, 200) resp_json = resp.json() self.assertEqual(resp_json['name'], new_party['name']) self.assertEqual(resp_json['type'], new_party['type']) self.assertEqual(resp_json['location'], new_party['location']) self.assertEqual(resp_json['leader_id'], self.user.id) id = resp_json['id'] party = Party.objects.get(id=id) self.assertEqual(party.name, new_party['name']) self.assertEqual(party.type, new_party['type']) self.assertEqual(party.location, new_party['location']) self.assertEqual(party.leader.id, self.user.id) def test_post_party_bad_request(self): self.login() resp = self.post('/api/party/', {}) self.assertEqual(resp.status_code, 400) def test_delete_party(self): party = Party( name="new party name", type=int(PartyType.Private), location="new party location", leader=self.user, ) party.save() id = party.id self.assertIsNotNone(cache.get('party:{}'.format(id))) state = party.get_state() self.assertIsNotNone(state) self.login() resp = self.delete('/api/party/{}/'.format(id)) self.assertEqual(resp.status_code, 200) party = Party.objects.filter(id=id) self.assertTrue(not party.exists()) self.assertIsNone(cache.get('party:{}'.format(id))) user = User.objects.create_user(username="******", email="*****@*****.**", password="******") user.save() party = Party(name="party", type=int(PartyType.InGroup), location="somewhere", leader=user) party.save() resp = self.delete('/api/party/{}/'.format(party.id)) self.assertEqual(resp.status_code, 403) state = party.get_state() state.delete() party.delete() def test_delete_party_with_invalid_state(self): party = Party( name="new party name", type=int(PartyType.Private), location="new party location", leader=self.user, ) party.save() id = party.id self.login() party.get_state().delete() resp = self.delete('/api/party/{}/'.format(id)) self.assertEqual(resp.status_code, 200) def test_get_party_detail(self): self.login() resp = self.get('/api/party/{}/'.format(self.party1.id)) self.assertEqual(resp.status_code, 200) resp_json = resp.json() self.assertDictEqual(resp_json, self.party1.as_dict()) def test_party_restaurant_field(self): self.assertEqual(self.party1.as_dict()['restaurant_id'], None) restaurant = Restaurant(name="Rustaurant") restaurant.save() self.party1.restaurant = restaurant self.party1.save() self.assertEqual(self.party1.as_dict()['restaurant_id'], restaurant.id)
def test_delete_party(self): party = Party( name="new party name", type=int(PartyType.Private), location="new party location", leader=self.user, ) party.save() id = party.id self.assertIsNotNone(cache.get('party:{}'.format(id))) state = party.get_state() self.assertIsNotNone(state) self.login() resp = self.delete('/api/party/{}/'.format(id)) self.assertEqual(resp.status_code, 200) party = Party.objects.filter(id=id) self.assertTrue(not party.exists()) self.assertIsNone(cache.get('party:{}'.format(id))) user = User.objects.create_user(username="******", email="*****@*****.**", password="******") user.save() party = Party(name="party", type=int(PartyType.InGroup), location="somewhere", leader=user) party.save() resp = self.delete('/api/party/{}/'.format(party.id)) self.assertEqual(resp.status_code, 403) state = party.get_state() state.delete() party.delete()
class UtilTestCase(TestCaseWithCache): def setUp(self): super().setUp() self.user = User.objects.create_user( email='*****@*****.**', password='******', username='******', ) self.party = Party( name="party 1 name", type=int(PartyType.Private), location="party 1 location", leader=self.user, ) self.party.save() self.state = self.party.get_state() cache.set('user-party:{}'.format(self.user.id), self.party.id) self.client.login(email=self.user.email, password='******') def test_get_party(self): (party, state) = get_party(self.state.id) self.assertEqual(party.as_dict(), self.party.as_dict()) self.assertEqual(state.as_dict(), self.state.as_dict()) def test_get_party_of_user(self): (party, state) = get_party_of_user(self.user.id) self.assertEqual(party.as_dict(), self.party.as_dict()) self.assertEqual(state.as_dict(), self.state.as_dict()) def test_get_party_invalid(self): with self.assertRaises(InvalidPartyError): get_party(0) def test_get_party_of_user_not_joined(self): cache.delete('user-party:{}'.format(self.user.id)) with self.assertRaises(NotJoinedError): get_party_of_user(self.user.id) def test_get_party_of_user_invalid(self): cache.set('user-party:{}'.format(self.user.id), 0) with self.assertRaises(InvalidPartyError): get_party_of_user(self.user.id) def test_delete_invalid_party(self): self.state.delete() Party.objects.get(id=self.party.id) self.assertIsNone(PartyState.get(self.state.id)) with self.assertRaises(InvalidPartyError): get_party(self.party.id) with self.assertRaises(Party.DoesNotExist): Party.objects.get(id=self.party.id) def test_delete_invalid_party_state(self): self.party.delete() with self.assertRaises(Party.DoesNotExist): Party.objects.get(id=self.party.id) self.assertIsNotNone(PartyState.get(self.state.id)) with self.assertRaises(InvalidPartyError): get_party(self.state.id) self.assertIsNone(PartyState.get(self.state.id))
class RecordTestCase(TestCaseWithHttp): def login(self): super().login(email="*****@*****.**", password="******") def setUp(self): self.user1 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.user2 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.user3 = User.objects.create_user( email="*****@*****.**", username="******", password="******", ) self.party = Party(name="rust party", location="rustland", type=int(PartyType.InGroup), leader=self.user1) self.party.save() self.state = self.party.get_state() self.restaurant = Restaurant(name="Rustaurant") self.restaurant.save() self.menu1 = Menu(name="Trait", price=10000) self.menu1.save() self.menu2 = Menu(name="Struct", price=33000) self.menu2.save() self.menu3 = Menu(name="Macro", price=13130) self.menu3.save() self.state.member_ids = [self.user1.id, self.user2.id, self.user3.id] self.state.member_ids_backup = [ self.user1.id, self.user2.id, self.user3.id ] self.state.menu_entries.add( self.menu1.id, 3, [self.user1.id, self.user2.id, self.user3.id]) self.state.menu_entries.add( self.menu2.id, 1, [self.user1.id, self.user2.id, self.user3.id]) self.state.menu_entries.add( self.menu3.id, 6, [self.user1.id, self.user2.id, self.user3.id]) self.state.phase = PartyPhase.PaymentAndCollection self.state.paid_user_id = self.user2.id self.state.restaurant_id = self.restaurant.id self.state.save() self.party.restaurant_id = self.restaurant.id self.party.save() def test_create_record(self): state = self.state record = make_record(state) self.assertEqual(record.name, self.party.name) self.assertEqual(record.type, self.party.type) self.assertEqual(record.location, self.party.location) self.assertEqual(record.leader, self.party.leader) self.assertEqual(record.since, self.party.since) self.assertListEqual([member.id for member in record.members.all()], [self.user1.id, self.user2.id, self.user3.id]) self.assertEqual(record.restaurant, self.party.restaurant) self.assertEqual(record.paid_user_id, state.paid_user_id) payments = record.payments.all() for payment in payments: self.assertTrue(payment.user_id in [self.user1.id, self.user3.id]) self.assertEqual(payment.paid_user_id, self.user2.id) if payment.menu_id == self.menu1.id: self.assertEqual(payment.price, self.menu1.price) elif payment.menu_id == self.menu2.id: self.assertEqual(payment.price, self.menu2.price / 3) else: self.assertEqual(payment.price, self.menu3.price * 2) def test_not_allowed(self): self.assertEqual(self.post('/api/party_records/', {}).status_code, 405) self.assertEqual(self.put('/api/party_records/', {}).status_code, 405) self.assertEqual(self.delete('/api/party_records/').status_code, 405) self.assertEqual(self.post('/api/payments/', {}).status_code, 405) self.assertEqual(self.put('/api/payments/', {}).status_code, 405) self.assertEqual(self.delete('/api/payments/').status_code, 405) self.assertEqual(self.post('/api/collections/', {}).status_code, 405) self.assertEqual(self.put('/api/collections/', {}).status_code, 405) self.assertEqual(self.delete('/api/collections/').status_code, 405) def test_unauthenticated(self): self.assertEqual(self.get('/api/party_records/').status_code, 403) self.assertEqual(self.get('/api/payments/').status_code, 403) self.assertEqual(self.get('/api/collections/').status_code, 403) def test_get_records(self): record = make_record(self.state) self.login() resp = self.get('/api/party_records/') self.assertEqual(resp.status_code, 200) resp_json = resp.json() self.assertEqual(resp_json[0]['id'], record.id) def test_get_payments(self): record = make_record(self.state) self.login() resp = self.get('/api/payments/') self.assertEqual(resp.status_code, 200) resp_json = resp.json() payments = record.payments.filter( user_id=self.user1.id).select_related('user', 'paid_user', 'menu').all() self.assertListEqual( [payment['id'] for payment in resp_json], [payment.id for payment in payments], ) self.assertListEqual( [payment['user'] for payment in resp_json], [payment.user.as_dict() for payment in payments], ) self.assertListEqual( [payment['paid_user'] for payment in resp_json], [payment.paid_user.as_dict() for payment in payments], ) self.assertListEqual( [payment['menu'] for payment in resp_json], [payment.menu.as_dict() for payment in payments], ) def test_get_collections(self): record = make_record(self.state) super().login(email="*****@*****.**", password="******") resp = self.get('/api/collections/') self.assertEqual(resp.status_code, 200) resp_json = resp.json() payments = record.payments.filter( paid_user_id=self.user2.id).select_related('user', 'paid_user', 'menu').all() self.assertListEqual( sorted([payment['id'] for payment in resp_json]), sorted([payment.id for payment in payments]), ) self.assertListEqual( sorted([payment['user'] for payment in resp_json], key=lambda u: u['id']), sorted([payment.user.as_dict() for payment in payments], key=lambda u: u['id']), ) self.assertListEqual( sorted([payment['paid_user'] for payment in resp_json], key=lambda u: u['id']), sorted([payment.paid_user.as_dict() for payment in payments], key=lambda u: u['id']), ) self.assertListEqual( sorted([payment['menu'] for payment in resp_json], key=lambda m: m['id']), sorted([payment.menu.as_dict() for payment in payments], key=lambda m: m['id']), ) def test_resolve_payment(self): record = make_record(self.state) super().login(email="*****@*****.**", password="******") resp = self.get('/api/resolve_payment/0/') self.assertEqual(resp.status_code, 404) for payment in record.payments.all(): resp = self.get('/api/resolve_payment/{}/'.format(payment.id)) self.assertEqual(resp.status_code, 200) self.assertFalse(record.payments.filter(resolved=False).exists()) resp = self.get('/api/collections/') self.assertListEqual(resp.json(), []) client = Client() client.login(email="*****@*****.**", password="******") resp = client.get('/api/payments/') self.assertListEqual(resp.json(), []) resp = client.get('/api/resolve_payment/{}/'.format( record.payments.all()[0].id)) self.assertEqual(resp.status_code, 403) def test_create_record_without_paid_user(self): state = self.state state.paid_user_id = None record = make_record(state) self.assertEqual(record.paid_user, None) payments = record.payments.all() for payment in payments: self.assertEqual(payment.paid_user, None) def test_create_record_with_api(self): super().login(email="*****@*****.**", password="******") records_length = PartyRecord.objects.all().count() self.delete('/api/party/{}/'.format(self.party.id)) self.assertEqual(PartyRecord.objects.all().count() - records_length, 1)