async def command_party_leave(self, msg): user = self.scope['user'] user_id = user.id (party, state) = get_party_of_user(user_id) party_id = party.id state.member_ids.remove(user_id) state.menu_entries.remove_user(user_id) party.member_count -= 1 state.save() party.save() cache.delete('user-party:{}'.format(user_id)) await self.channel_layer.group_discard( 'party-{}'.format(party_id), self.channel_name, ) if party.member_count > 0: await self.channel_layer.group_send('party-{}'.format(party_id), event.party_leave(user_id)) if party.leader_id == user_id: next_user_id = state.member_ids[0] party.leader_id = next_user_id party.save() await self.send_json(event.leader_change(next_user_id), ) else: make_record(state) state.delete() party.delete()
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_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_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_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], )
async def command_party_delete(self, data): user = self.scope['user'] user_id = user.id (party, state) = get_party_of_user(user_id) if party.leader_id != user_id: raise exception.NotAuthorizedError party_id = party.id await self.channel_layer.group_send( 'party-{}'.format(party_id), event.party_delete(), ) make_record(state) state.delete() party.delete()
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_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)