def test_creating_change(self): self.assertFalse(Change.list(channel=self.channel)) self.client.post(self.url) self.assertFalse(Change.list(channel=self.channel)) self.client.post(self.url, {'channel': self.channel.name}) changes = Change.list(channel=self.channel) self.assertTrue(changes) self.assertEqual(changes[0].action, Change.ACTIONS.CREATE)
def data_apiview_processed_handler(sender, view, instance, action, **kwargs): if instance.channel_id is None: return action = Change.ACTIONS(action).value request = view.request channel = instance.channel room = instance.channel_room view.check_channel_permission(request, channel) author = create_author_dict(request) metadata = {'type': 'object', 'class': view.klass.name} changes = getattr(instance, 'changes', None) if changes is not None: changes.add('id') payload = view.serializer_class(instance, fields=changes, excluded_fields=('links', 'channel', 'channel_room')).data channel.create_change(room=room, author=author, metadata=metadata, payload=payload, action=action)
def test_changes_on_delete(self): self.client.delete(self.url) changes = Change.list(channel=self.channel) self.assertTrue(changes) self.assertEqual(changes[0].action, Change.ACTIONS.DELETE) self.assertEqual(changes[0].payload, {'id': self.object.id})
def process_poll(self, request, channel, last_id, room): # Filter by last id if needed if last_id is not None: # Filter by last_id by id change_list = Change.list(min_pk=last_id + 1, ordering='asc', limit=1, channel=channel, room=room) if change_list: return Response( ChangeSerializer(change_list[0], excluded_fields=('links', 'room',)).data) # No results found, return an async_tasks handler to subscribe and wait for results return self.create_uwsgi_response(request, channel, last_id, room)
def test_changes_on_update(self): data = {'string': 'test123', 'integer': 123} response = self.client.patch(self.url, data) changes = Change.list(channel=self.channel) self.assertTrue(changes) self.assertEqual(changes[0].action, Change.ACTIONS.UPDATE) data['revision'] = response.data['revision'] data['updated_at'] = response.data['updated_at'] data['id'] = response.data['id'] self.assertEqual(changes[0].payload, data)
def test_publish_permission_is_validated(self): response = self.client.post(self.url, {'channel': self.channel.name}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertIn('channel', response.data['detail']) G(Membership, user=self.user, group=self.group_2) response = self.client.post(self.url, {'channel': self.channel.name}) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Verify serializer version changes = Change.list(channel=self.channel) self.assertTrue(changes) self.assertIn('acl', changes[-1].payload)
def get_change_from_database(self, environ, last_id, limit=1): """ Process change from database. """ if last_id is None: return channel_pk = int(environ['CHANNEL_PK']) instance_pk = int(environ['INSTANCE_PK']) channel_room = environ.get('CHANNEL_ROOM') change_list = Change.list(min_pk=last_id + 1, ordering='asc', limit=limit, channel=Munch(id=channel_pk), instance=Munch(id=instance_pk), room=channel_room) for change in change_list: yield change