def test_post_data_errors(self):
        json_data = ''
        response = self._post(json_data)
        self.assertEqual(response.status_code, 400)
        self.assertIn(b'Invalid JSON', response.content)

        json_data = {}
        response = self._post(json_data)
        self.assertEqual(response.status_code, 400)
        self.assertIn(b'Invalid JSON', response.content)

        json_data = {'message': {}}
        response = self._post(json_data)
        self.assertEqual(response.status_code, 400)
        self.assertIn(b'Invalid JSON', response.content)

        json_data = {'message': {'content': '', 'tags': ['Bothell']}}
        response = self._post(json_data)
        self.assertEqual(response.status_code, 400)
        self.assertIn(b"Invalid tag: Bothell", response.content)

        json_data = {
            'message': {
                'content': '',
                'expires': mocked_current_datetime() - timedelta(days=7),
            }
        }
        response = self._post(json_data)
        self.assertEqual(response.status_code, 400)
        self.assertIn(b"Invalid expires: expires precedes begins",
                      response.content)
示例#2
0
    def test_message_active(self, mock_dt):
        # Expires not defined
        self.message.save()
        self.assertEqual(self.message.begins.isoformat(),
                         '2018-01-01T10:10:10+00:00')
        self.assertEqual(self.message.expires, None)
        self.assertEqual(self.message.is_active(), True)

        # Begins is past, expires is future
        self.message.expires = mocked_current_datetime() + timedelta(days=7)
        self.message.save()
        self.assertEqual(self.message.begins.isoformat(),
                         '2018-01-01T10:10:10+00:00')
        self.assertEqual(self.message.expires.isoformat(),
                         '2018-01-08T10:10:10+00:00')
        self.assertEqual(self.message.is_active(), True)

        # Begins and expires are past
        self.message.begins = mocked_current_datetime() - timedelta(days=15)
        self.message.expires = mocked_current_datetime() - timedelta(days=7)
        self.message.save()
        self.assertEqual(self.message.begins.isoformat(),
                         '2017-12-17T10:10:10+00:00')
        self.assertEqual(self.message.expires.isoformat(),
                         '2017-12-25T10:10:10+00:00')
        self.assertEqual(self.message.is_active(), False)

        # Begins and expires are future
        self.message.begins = mocked_current_datetime() + timedelta(days=1)
        self.message.expires = mocked_current_datetime() + timedelta(days=7)
        self.message.save()
        self.assertEqual(self.message.begins.isoformat(),
                         '2018-01-02T10:10:10+00:00')
        self.assertEqual(self.message.expires.isoformat(),
                         '2018-01-08T10:10:10+00:00')
        self.assertEqual(self.message.is_active(), False)
示例#3
0
    def setUp(self, mock_dt):
        tag1 = Tag.objects.get(name='Seattle')
        tag2 = Tag.objects.get(name='Tacoma')

        message1 = Message(content='1')
        message1.save()
        message1.tags.add(tag1)

        message2 = Message(content='2')
        message2.begins = mocked_current_datetime() + timedelta(days=7)
        message2.save()
        message2.tags.add(tag2)

        message3 = Message(content='3')
        message3.save()

        message4 = Message(content='4', level=Message.WARNING_LEVEL)
        message4.save()
        message4.tags.add(tag1, tag2)
    def test_put(self, mock_dt):
        message = Message.objects.get(content='4')
        json_data = message.to_json()

        self.assertEqual(json_data['content'], '4')
        self.assertEqual(json_data['level'], Message.WARNING_LEVEL)
        self.assertEqual(json_data['tags'], [{
            'group': 'Cities',
            'id': 3,
            'name': 'Seattle'
        }, {
            'group': 'Cities',
            'id': 4,
            'name': 'Tacoma'
        }])

        json_data['content'] = 'abc'
        json_data['level'] = Message.DANGER_LEVEL
        json_data['expires'] = mocked_current_datetime() + timedelta(days=7)
        json_data['tags'] = []  # Remove all tags

        url = reverse('message_api', kwargs={'message_id': message.pk})
        request = self.factory.put(url,
                                   data={'message': json_data},
                                   content_type='application/json')
        request.user = self.user
        response = MessageAPI.as_view()(request, message_id=message.pk)

        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data.get('message').get('content'), 'abc')
        self.assertEqual(
            data.get('message').get('level'), Message.DANGER_LEVEL)
        self.assertEqual(
            data.get('message').get('expires'), '2018-01-08T10:10:10+00:00')
        self.assertEqual(json_data['tags'], [])