Esempio n. 1
0
    def test_edit_event(self):
        """Should return 200 with updated event info"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {
            'HTTP_AUTHORIZATION': f'JWT {token}',
            'CONTENT_TYPE': 'application/json',  # required for patch
        }

        new_end_time = datetime(year=2019, month=10, day=16, hour=1)
        payload = json.dumps({
            'name':
            'TESTING WHEEE!!!!',
            'ends_at':
            datetime.strftime(new_end_time, '%Y-%m-%d %H:%M:%S')
        })

        resp = self.client.patch('/api/events/80001/', payload, **headers)

        self.assertContainsJSON(resp, {
            'name': 'TESTING WHEEE!!!!',
            'description': 'This is an event for testing purposes',
            'owner_id': 70001,
            'ends_at': '2019-10-16T01:00:00Z'
        },
                                status_code=200)
    def test_unregister(self):
        """Should return 204 (No Content ~= deleted)"""
        user = User.objects.get(pk=70002)  # 2nd user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.delete('/api/events/80001/register', **headers)

        self.assertContainsJSON(resp, None, status_code=204)
    def test_unregister__invalid_event(self):
        """Should return 404 (Event record not found)"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.delete('/api/events/80009/register', **headers)

        self.assertContainsJSON(resp, {'detail': 'Event not found.'},
                                status_code=404)
Esempio n. 4
0
    def test_get_event__notfound(self):
        """Should return 404"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.get('/api/events/80501/', **headers)

        self.assertContainsJSON(resp, {'detail': 'Not found.'},
                                status_code=404)
    def test_unregister__not_registered(self):
        """Should return 404 (Attendance record not found)"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.delete('/api/events/80001/register', **headers)

        self.assertContainsJSON(
            resp, {'detail': 'You have not registered for this event.'},
            status_code=404)
    def test_register__dupe(self):
        """Should return 400 (duplicate)"""
        user = User.objects.get(pk=70002)  # 2nd user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.post('/api/events/80001/register', **headers)

        self.assertContainsJSON(
            resp, {'detail': 'You are already registered for this event.'},
            status_code=400)
    def test_register(self):
        """Should return 201 (Created)"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.post('/api/events/80001/register', **headers)

        self.assertContainsJSON(resp, {
            'created_at': SpecHelpers.is_datetime,
            'event': 80001,
            'user': 70001
        },
                                status_code=201)
Esempio n. 8
0
    def test_create_event__missing_fields(self):
        """Should return 400 with field errors"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        payload = {'name': 'TIMELESS PARTY'}

        resp = self.client.post('/api/events/', payload, **headers)

        self.assertContainsJSON(resp, {
            'starts_at': ['This field is required.'],
            'ends_at': ['This field is required.'],
        },
                                status_code=400)
Esempio n. 9
0
    def test_get_event(self):
        """Should return 200 with the event data"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        resp = self.client.get('/api/events/80001/', **headers)

        self.assertContainsJSON(
            resp,
            {
                'pk': 80001,
                'name': 'Test Event',
                'image': 'http://example.com/event-image.png',
                'starts_at': '2019-10-15T08:00:00Z'  # != fixture? normalized!
            })
Esempio n. 10
0
    def test_create_event__invalid_time(self):
        """Should return 400 with error details"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        payload = {
            'name': 'New Years Bonanza!',
            'starts_at': datetime(year=2019, month=12, day=31, hour=22),
            'ends_at': datetime(year=2018, month=1, day=1,
                                hour=2)  # 2 years b4
        }

        resp = self.client.post('/api/events/', payload, **headers)

        self.assertContainsJSON(resp,
                                {'ends_at': ['End time before start time.']},
                                status_code=400)
Esempio n. 11
0
    def test_create_event__no_start_time(self):
        """Should return 400 with field errors

        Added to test an edge case with end_data validation
        """
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        payload = {
            'name': 'ETERNAL PARTY',
            'ends_at': datetime(year=2020, month=1, day=1, hour=2)
        }

        resp = self.client.post('/api/events/', payload, **headers)

        self.assertContainsJSON(resp, {
            'starts_at': ['This field is required.'],
        },
                                status_code=400)
Esempio n. 12
0
    def test_create_event(self):
        """Should return 201 with event data"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        payload = {
            'name': 'New Years Bonanza!',
            'starts_at': datetime(year=2019, month=12, day=31, hour=22),
            'ends_at': datetime(year=2020, month=1, day=1, hour=2)
        }

        resp = self.client.post('/api/events/', payload, **headers)

        self.assertContainsJSON(resp, {
            'name': 'New Years Bonanza!',
            'description': '',
            'owner_id': 70001,
            'ends_at': '2020-01-01T02:00:00Z'
        },
                                status_code=201)
Esempio n. 13
0
    def test_create_event__try_set_pk(self):
        """Should return 201, but not take the provided pk"""
        user = User.objects.get(pk=70001)  # test user
        token = make_token(user)
        headers = {'HTTP_AUTHORIZATION': f'JWT {token}'}

        payload = {
            'pk': 80032,
            'name': 'New Years Bonanza!',
            'starts_at': datetime(year=2019, month=12, day=31, hour=22),
            'ends_at': datetime(year=2020, month=1, day=1, hour=2)
        }

        resp = self.client.post('/api/events/', payload, **headers)

        self.assertContainsJSON(
            resp,
            {
                'pk': lambda x: x != 80032,  # check pk not set to spec'd val
                'owner_id': 70001,
            },
            status_code=201)
Esempio n. 14
0
    def test_edit_event__not_owner(self):
        """Should return 403"""
        user = User.objects.get(pk=70002)  # 2nd test user
        token = make_token(user)
        headers = {
            'HTTP_AUTHORIZATION': f'JWT {token}',
            'CONTENT_TYPE': 'application/json',  # required for patch
        }

        new_end_time = datetime(year=2019, month=10, day=16, hour=1)
        payload = json.dumps({
            'name':
            'TESTING WHEEE!!!!',
            'ends_at':
            datetime.strftime(new_end_time, '%Y-%m-%d %H:%M:%S')
        })

        resp = self.client.patch('/api/events/80001/', payload, **headers)

        self.assertContainsJSON(
            resp,
            {'detail': 'You do not have permission to perform this action.'},
            status_code=403)