Beispiel #1
0
class TalkUrlsViewSetPermissionTests(TestCase):

    def setUp(self):
        self.talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        self.talk_r = create_talk("Talk R", REJECTED, "author_r")
        self.talk_s = create_talk("Talk S", SUBMITTED, "author_s")
        self.client = SortedResultsClient(sort_key="url")

    def assert_urls_accessible(self, talk):
        response = self.client.get(
            '/talks/api/talks/%d/urls/' % talk.talk_id)
        self.assertEqual(response.data['results'], [])

    def assert_urls_forbidden(self, talk):
        response = self.client.get(
            '/talks/api/talks/%d/urls/' % talk.talk_id)
        self.assertEqual(response.status_code, 403)

    def assert_missing_talk_urls_code(self, code):
        missing_talk_id = 4242  # implausibly large id
        response = self.client.get(
            '/talks/api/talks/%d/urls/' % missing_talk_id)
        self.assertEqual(response.status_code, code)

    def test_unauthorized_users_get_no_talk_urls(self):
        self.assert_urls_forbidden(self.talk_a)
        self.assert_urls_forbidden(self.talk_s)
        self.assert_urls_forbidden(self.talk_r)
        self.assert_missing_talk_urls_code(403)

    def test_ordinary_users_get_no_talk_urls(self):
        create_user('norm')
        self.assert_urls_forbidden(self.talk_a)
        self.assert_urls_forbidden(self.talk_s)
        self.assert_urls_forbidden(self.talk_r)
        self.assert_missing_talk_urls_code(403)

    def test_super_user_gets_all_talk_urls(self):
        create_user('super', True)
        self.client.login(username='******', password='******')
        self.assert_urls_accessible(self.talk_a)
        self.assert_urls_accessible(self.talk_s)
        self.assert_urls_accessible(self.talk_r)
        self.assert_missing_talk_urls_code(404)
Beispiel #2
0
 def setUp(self):
     create_user('super', True)
     self.client = SortedResultsClient(sort_key="url")
     self.client.login(username='******', password='******')
Beispiel #3
0
class TalkUrlsViewSetTests(TestCase):
    def setUp(self):
        create_user('super', True)
        self.client = SortedResultsClient(sort_key="url")
        self.client.login(username='******', password='******')

    def mk_result(self, talk_url):
        return {
            'id': talk_url.id,
            'description': talk_url.description,
            'url': talk_url.url,
        }

    def test_list_talk_urls(self):
        talk = create_talk("Talk", ACCEPTED, "author")
        url_a = TalkUrl.objects.create(talk=talk,
                                       url="http://a.example.com/",
                                       description="video")
        url_b = TalkUrl.objects.create(talk=talk,
                                       url="http://b.example.com/",
                                       description="slides")
        response = self.client.get('/talks/api/talks/%d/urls/' % talk.talk_id)
        self.assertEqual(response.data['results'], [
            self.mk_result(url_a),
            self.mk_result(url_b),
        ])

    def test_retrieve_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(talk=talk,
                                     url="http://a.example.com/",
                                     description="video")
        response = self.client.get('/talks/api/talks/%d/urls/%d/' %
                                   (talk.talk_id, url.id))
        self.assertEqual(response.data, self.mk_result(url))

    def test_create_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.post('/talks/api/talks/%d/urls/' % talk.talk_id,
                                    data={
                                        'description': u'slides',
                                        'url': u'http://www.example.com/video',
                                    },
                                    format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://www.example.com/video')
        self.assertEqual(talk_url.description, u'slides')

    def test_update_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(talk=talk,
                                     url="http://a.example.com/",
                                     description="video")
        response = self.client.put('/talks/api/talks/%d/urls/%d/' %
                                   (talk.talk_id, url.id),
                                   data={
                                       'description': u'slides',
                                       'url': u'http://www.example.com/video',
                                   },
                                   format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://www.example.com/video')
        self.assertEqual(talk_url.description, u'slides')

    def test_patch_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(talk=talk,
                                     url="http://a.example.com/",
                                     description="video")
        response = self.client.patch('/talks/api/talks/%d/urls/%d/' %
                                     (talk.talk_id, url.id),
                                     data={
                                         'url': 'http://new.example.com/',
                                     },
                                     format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://new.example.com/')
        self.assertEqual(talk_url.description, u'video')

    def test_delete_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url_a = TalkUrl.objects.create(talk=talk,
                                       url="http://a.example.com/",
                                       description="video")
        url_b = TalkUrl.objects.create(talk=talk,
                                       url="http://a.example.com/",
                                       description="video")
        response = self.client.delete('/talks/api/talks/%d/urls/%d/' %
                                      (talk.talk_id, url_a.id))
        [talk_url_b] = talk.talkurl_set.all()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(talk_url_b, url_b)
Beispiel #4
0
 def setUp(self):
     self.talk_a = create_talk("Talk A", ACCEPTED, "author_a")
     self.talk_r = create_talk("Talk R", REJECTED, "author_r")
     self.talk_s = create_talk("Talk S", SUBMITTED, "author_s")
     self.client = SortedResultsClient(sort_key="url")
Beispiel #5
0
class TalkViewSetTests(TestCase):
    def setUp(self):
        create_user('super', True)
        self.client = SortedResultsClient(sort_key="title")
        self.client.login(username='******', password='******')

    def mk_result(self, talk):
        def mk_url(talk_url):
            return {
                'id': talk_url.id,
                'description': talk_url.description,
                'url': talk_url.url,
            }

        return {
            'talk_id': talk.talk_id,
            'talk_type': talk.talk_type,
            'status': talk.status,
            'title': talk.title,
            'abstract': talk.abstract.raw,
            'corresponding_author': talk.corresponding_author.id,
            'authors': [talk.corresponding_author.id],
            'kv': [],
            'urls': [mk_url(url) for url in talk.talkurl_set.all()]
        }

    def test_list_talks(self):
        talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        talk_b = create_talk("Talk B", REJECTED, "author_b")
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['results'], [
            self.mk_result(talk_a),
            self.mk_result(talk_b),
        ])

    def test_retrieve_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        talk.abstract = "Abstract Talk A"
        talk.save()
        TalkUrl.objects.create(talk=talk,
                               url="http://example.com/",
                               description="video")
        response = self.client.get('/talks/api/talks/%d/' % talk.talk_id)
        self.assertEqual(response.data, self.mk_result(talk))

    def test_create_talk(self):
        author = create_user("author")
        response = self.client.post('/talks/api/talks/',
                                    data={
                                        'talk_type': None,
                                        'status': 'A',
                                        'title': 'Talk Foo',
                                        'abstract': 'Concrete',
                                        'corresponding_author': author.id,
                                        'authors': [author.id],
                                    },
                                    format='json')
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))

    def test_update_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.put('/talks/api/talks/%d/' % talk.talk_id,
                                   data={
                                       'talk_type':
                                       None,
                                       'status':
                                       'R',
                                       'title':
                                       'Talk Zoom',
                                       'abstract':
                                       'Concreter',
                                       'corresponding_author':
                                       talk.corresponding_author.id,
                                       'authors':
                                       [talk.corresponding_author.id],
                                   },
                                   format="json")
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))
        self.assertEqual(talk.abstract.raw, u"Concreter")

    def test_patch_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.patch('/talks/api/talks/%d/' % talk.talk_id,
                                     data={
                                         'abstract': 'Concrete',
                                     },
                                     format="json")
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))
        self.assertEqual(talk.abstract.raw, u"Concrete")

    def test_delete_talk(self):
        talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        talk_b = create_talk("Talk B", ACCEPTED, "author_b")
        response = self.client.delete('/talks/api/talks/%d/' % talk_a.talk_id)
        talk_remaining = Talk.objects.get()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(talk_remaining, talk_b)
Beispiel #6
0
class TalkViewSetPermissionTests(TestCase):
    def setUp(self):
        self.talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        self.talk_r = create_talk("Talk R", REJECTED, "author_r")
        self.talk_s = create_talk("Talk S", SUBMITTED, "author_s")
        self.client = SortedResultsClient(sort_key="title")

    def test_unauthorized_users(self):
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_r.talk_id)
        self.assertEqual(response.status_code, 404)

    def test_ordinary_users_get_accepted_talks(self):
        create_user('norm')
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_r.talk_id)
        self.assertEqual(response.status_code, 404)

    def test_super_user_gets_everything(self):
        create_user('super', True)
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 3)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")
        self.assertEqual(response.data['results'][2]['title'], "Talk S")
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_r.talk_id)
        self.assertEqual(response.data['title'], 'Talk R')

    def test_reviewer_all_talks(self):
        create_user('reviewer', perms=['view_all_talks'])
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 3)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")
        self.assertEqual(response.data['results'][2]['title'], "Talk S")
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get('/talks/api/talks/%d/' %
                                   self.talk_r.talk_id)
        self.assertEqual(response.data['title'], 'Talk R')

    def test_author_a_sees_own_talks_only(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")

    def test_author_r_sees_own_talk(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")

    def test_author_s_sees_own_talk(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk S")
Beispiel #7
0
 def setUp(self):
     create_user('super', True)
     self.client = SortedResultsClient(sort_key="url")
     self.client.login(username='******', password='******')
Beispiel #8
0
class TalkUrlsViewSetTests(TestCase):

    def setUp(self):
        create_user('super', True)
        self.client = SortedResultsClient(sort_key="url")
        self.client.login(username='******', password='******')

    def mk_result(self, talk_url):
        return {
            'id': talk_url.id, 'description': talk_url.description,
            'url': talk_url.url,
        }

    def test_list_talk_urls(self):
        talk = create_talk("Talk", ACCEPTED, "author")
        url_a = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        url_b = TalkUrl.objects.create(
            talk=talk, url="http://b.example.com/", description="slides")
        response = self.client.get('/talks/api/talks/%d/urls/' % talk.talk_id)
        self.assertEqual(response.data['results'], [
            self.mk_result(url_a), self.mk_result(url_b),
        ])

    def test_retrieve_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        response = self.client.get(
            '/talks/api/talks/%d/urls/%d/' % (talk.talk_id, url.id))
        self.assertEqual(response.data, self.mk_result(url))

    def test_create_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.post(
            '/talks/api/talks/%d/urls/' % talk.talk_id, data={
                'description': u'slides',
                'url': u'http://www.example.com/video',
            }, format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://www.example.com/video')
        self.assertEqual(talk_url.description, u'slides')

    def test_update_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        response = self.client.put(
            '/talks/api/talks/%d/urls/%d/' % (talk.talk_id, url.id), data={
                'description': u'slides',
                'url': u'http://www.example.com/video',
            }, format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://www.example.com/video')
        self.assertEqual(talk_url.description, u'slides')

    def test_patch_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        response = self.client.patch(
            '/talks/api/talks/%d/urls/%d/' % (talk.talk_id, url.id), data={
                'url': 'http://new.example.com/',
            }, format="json")
        [talk_url] = talk.talkurl_set.all()
        self.assertEqual(response.data, self.mk_result(talk_url))
        self.assertEqual(talk_url.url, u'http://new.example.com/')
        self.assertEqual(talk_url.description, u'video')

    def test_delete_talk_url(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        url_a = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        url_b = TalkUrl.objects.create(
            talk=talk, url="http://a.example.com/", description="video")
        response = self.client.delete(
            '/talks/api/talks/%d/urls/%d/' % (talk.talk_id, url_a.id))
        [talk_url_b] = talk.talkurl_set.all()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(talk_url_b, url_b)
Beispiel #9
0
 def setUp(self):
     self.talk_a = create_talk("Talk A", ACCEPTED, "author_a")
     self.talk_r = create_talk("Talk R", REJECTED, "author_r")
     self.talk_s = create_talk("Talk S", SUBMITTED, "author_s")
     self.client = SortedResultsClient(sort_key="url")
Beispiel #10
0
class TalkViewSetTests(TestCase):

    def setUp(self):
        create_user('super', True)
        self.client = SortedResultsClient(sort_key="title")
        self.client.login(username='******', password='******')

    def mk_result(self, talk):
        def mk_url(talk_url):
            return {
                'id': talk_url.id, 'description': talk_url.description,
                'url': talk_url.url,
            }
        return {
            'talk_id': talk.talk_id, 'talk_type': talk.talk_type,
            'status': talk.status, 'title': talk.title,
            'abstract': talk.abstract.raw,
            'corresponding_author': talk.corresponding_author.id,
            'authors': [talk.corresponding_author.id],
            'kv': [],
            'urls': [mk_url(url) for url in talk.talkurl_set.all()]
        }

    def test_list_talks(self):
        talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        talk_b = create_talk("Talk B", REJECTED, "author_b")
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['results'], [
            self.mk_result(talk_a), self.mk_result(talk_b),
        ])

    def test_retrieve_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        talk.abstract = "Abstract Talk A"
        talk.save()
        TalkUrl.objects.create(
            talk=talk, url="http://example.com/", description="video")
        response = self.client.get(
            '/talks/api/talks/%d/' % talk.talk_id)
        self.assertEqual(response.data, self.mk_result(talk))

    def test_create_talk(self):
        author = create_user("author")
        response = self.client.post('/talks/api/talks/', data={
            'talk_type': None, 'status': 'A', 'title': 'Talk Foo',
            'abstract': 'Concrete',
            'corresponding_author': author.id,
            'authors': [author.id],
        }, format='json')
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))

    def test_update_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.put(
            '/talks/api/talks/%d/' % talk.talk_id, data={
                'talk_type': None, 'status': 'R', 'title': 'Talk Zoom',
                'abstract': 'Concreter',
                'corresponding_author': talk.corresponding_author.id,
                'authors': [talk.corresponding_author.id],
            }, format="json")
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))
        self.assertEqual(talk.abstract.raw, u"Concreter")

    def test_patch_talk(self):
        talk = create_talk("Talk A", ACCEPTED, "author_a")
        response = self.client.patch(
            '/talks/api/talks/%d/' % talk.talk_id, data={
                'abstract': 'Concrete',
            }, format="json")
        talk = Talk.objects.get()
        self.assertEqual(response.data, self.mk_result(talk))
        self.assertEqual(talk.abstract.raw, u"Concrete")

    def test_delete_talk(self):
        talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        talk_b = create_talk("Talk B", ACCEPTED, "author_b")
        response = self.client.delete('/talks/api/talks/%d/' % talk_a.talk_id)
        talk_remaining = Talk.objects.get()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(talk_remaining, talk_b)
Beispiel #11
0
class TalkViewSetPermissionTests(TestCase):

    def setUp(self):
        self.talk_a = create_talk("Talk A", ACCEPTED, "author_a")
        self.talk_r = create_talk("Talk R", REJECTED, "author_r")
        self.talk_s = create_talk("Talk S", SUBMITTED, "author_s")
        self.client = SortedResultsClient(sort_key="title")

    def test_unauthorized_users(self):
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_r.talk_id)
        self.assertEqual(response.status_code, 404)

    def test_ordinary_users_get_accepted_talks(self):
        create_user('norm')
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_r.talk_id)
        self.assertEqual(response.status_code, 404)

    def test_super_user_gets_everything(self):
        create_user('super', True)
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 3)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")
        self.assertEqual(response.data['results'][2]['title'], "Talk S")
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_r.talk_id)
        self.assertEqual(response.data['title'], 'Talk R')

    def test_reviewer_all_talks(self):
        create_user('reviewer', perms=['view_all_talks'])
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 3)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")
        self.assertEqual(response.data['results'][2]['title'], "Talk S")
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_a.talk_id)
        self.assertEqual(response.data['title'], 'Talk A')
        response = self.client.get(
            '/talks/api/talks/%d/' % self.talk_r.talk_id)
        self.assertEqual(response.data['title'], 'Talk R')

    def test_author_a_sees_own_talks_only(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")

    def test_author_r_sees_own_talk(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk R")

    def test_author_s_sees_own_talk(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/talks/api/talks/')
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['title'], "Talk A")
        self.assertEqual(response.data['results'][1]['title'], "Talk S")
Beispiel #12
0
 def setUp(self):
     create_user("super", superuser=True)
     self.client = SortedResultsClient(sort_key="barcode")
     self.client.login(username="******", password="******")
Beispiel #13
0
class TicketsViewSetTests(TestCase):
    def setUp(self):
        create_user("super", superuser=True)
        self.client = SortedResultsClient(sort_key="barcode")
        self.client.login(username="******", password="******")

    def mk_result(self, ticket):
        return {
            "barcode": ticket.barcode,
            "email": ticket.email,
            "type": ticket.type.id,
            "user": None,
        }

    def test_list_tickets(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket_1 = Ticket.objects.create(barcode=1,
                                         email="*****@*****.**",
                                         type=ticket_type)
        ticket_2 = Ticket.objects.create(barcode=2,
                                         email="*****@*****.**",
                                         type=ticket_type)
        response = self.client.get("/tickets/api/tickets/")
        self.assertEqual(response.data["results"], [
            self.mk_result(ticket_1),
            self.mk_result(ticket_2),
        ])

    def test_retrieve_ticket(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket = Ticket.objects.create(barcode=1,
                                       email="*****@*****.**",
                                       type=ticket_type)
        response = self.client.get("/tickets/api/tickets/%d/" %
                                   (ticket.barcode, ))
        self.assertEqual(response.data, self.mk_result(ticket))

    def test_create_ticket(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        response = self.client.post(
            "/tickets/api/tickets/",
            data={
                "barcode": 123,
                "email": "*****@*****.**",
                "type": ticket_type.id,
                "user": None,
            },
            format="json",
        )
        self.assertEqual(response.status_code, 201)
        [ticket] = Ticket.objects.all()
        self.assertEqual(response.data, self.mk_result(ticket))
        self.assertEqual(ticket.barcode, 123)
        self.assertEqual(ticket.email, "*****@*****.**")
        self.assertEqual(ticket.type, ticket_type)
        self.assertEqual(ticket.user, None)

    def test_create_ticket_without_barcode(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        response = self.client.post(
            "/tickets/api/tickets/",
            data={
                "email": "*****@*****.**",
                "type": ticket_type.id,
                "user": None,
            },
            format="json",
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(),
                         ["barcode required during ticket creation"])
        self.assertEqual(Ticket.objects.count(), 0)

    def test_update_ticket(self):
        ticket_type_1 = TicketType.objects.create(name="Test Type 1")
        ticket_type_2 = TicketType.objects.create(name="Test Type 1")
        ticket = Ticket.objects.create(barcode=123,
                                       email="*****@*****.**",
                                       type=ticket_type_1)
        response = self.client.put(
            "/tickets/api/tickets/%d/" % (ticket.barcode, ),
            data={
                "email": "*****@*****.**",
                "type": ticket_type_2.id,
                "user": None,
            },
            format="json",
        )
        [ticket] = Ticket.objects.all()
        self.assertEqual(ticket.barcode, 123)
        self.assertEqual(response.data, self.mk_result(ticket))
        self.assertEqual(ticket.email, "*****@*****.**")
        self.assertEqual(ticket.type, ticket_type_2)
        self.assertEqual(ticket.user, None)

    def test_update_ticket_with_barcode(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket = Ticket.objects.create(barcode=123,
                                       email="*****@*****.**",
                                       type=ticket_type)
        response = self.client.put(
            "/tickets/api/tickets/%d/" % (ticket.barcode, ),
            data={
                "barcode": 456,
                "email": "*****@*****.**",
                "type": ticket_type.id,
                "user": None,
            },
            format="json",
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(),
                         ["barcode forbidden during ticket update"])
        [ticket_new] = Ticket.objects.all()
        self.assertEqual(ticket_new, ticket)

    def test_patch_ticket(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket = Ticket.objects.create(barcode=123,
                                       email="*****@*****.**",
                                       type=ticket_type)
        response = self.client.patch(
            "/tickets/api/tickets/%d/" % (ticket.barcode, ),
            data={
                "email": "*****@*****.**",
            },
            format="json",
        )
        [ticket] = Ticket.objects.all()
        self.assertEqual(ticket.barcode, 123)
        self.assertEqual(response.data, self.mk_result(ticket))
        self.assertEqual(ticket.email, "*****@*****.**")
        self.assertEqual(ticket.type, ticket_type)
        self.assertEqual(ticket.user, None)

    def test_delete_ticket(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket_1 = Ticket.objects.create(barcode=123,
                                         email="*****@*****.**",
                                         type=ticket_type)
        ticket_2 = Ticket.objects.create(barcode=456,
                                         email="*****@*****.**",
                                         type=ticket_type)
        response = self.client.delete(
            "/tickets/api/tickets/%d/" % (ticket_1.barcode, ), )
        [ticket] = Ticket.objects.all()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(ticket, ticket_2)
Beispiel #14
0
class TicketTypesViewSetTests(TestCase):
    def setUp(self):
        create_user("super", superuser=True)
        self.client = SortedResultsClient(sort_key="name")
        self.client.login(username="******", password="******")

    def mk_result(self, ticket_type):
        return {
            "id": ticket_type.id,
            "name": ticket_type.name,
        }

    def test_list_ticket_types(self):
        ticket_type_1 = TicketType.objects.create(name="Test Type 1")
        ticket_type_2 = TicketType.objects.create(name="Test Type 2")
        response = self.client.get("/tickets/api/tickettypes/")
        self.assertEqual(response.data["results"], [
            self.mk_result(ticket_type_1),
            self.mk_result(ticket_type_2),
        ])

    def test_retrieve_ticket_type(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        response = self.client.get("/tickets/api/tickettypes/%d/" %
                                   (ticket_type.id, ))
        self.assertEqual(response.data, self.mk_result(ticket_type))

    def test_create_ticket_type(self):
        response = self.client.post(
            "/tickets/api/tickettypes/",
            data={"name": "New Ticket Type"},
            format="json",
        )
        [ticket_type] = TicketType.objects.all()
        self.assertEqual(response.data, self.mk_result(ticket_type))
        self.assertEqual(ticket_type.name, "New Ticket Type")

    def test_update_ticket_type(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket_type_id = ticket_type.id
        response = self.client.put(
            "/tickets/api/tickettypes/%d/" % (ticket_type.id, ),
            data={"name": "New Type Name"},
            format="json",
        )
        [ticket_type] = TicketType.objects.all()
        self.assertEqual(ticket_type.id, ticket_type_id)
        self.assertEqual(response.data, self.mk_result(ticket_type))
        self.assertEqual(ticket_type.name, "New Type Name")

    def test_patch_ticket_type(self):
        ticket_type = TicketType.objects.create(name="Test Type 1")
        ticket_type_id = ticket_type.id
        response = self.client.patch(
            "/tickets/api/tickettypes/%d/" % (ticket_type.id, ),
            data={"name": "New Type Name"},
            format="json",
        )
        [ticket_type] = TicketType.objects.all()
        self.assertEqual(ticket_type.id, ticket_type_id)
        self.assertEqual(response.data, self.mk_result(ticket_type))
        self.assertEqual(ticket_type.name, "New Type Name")

    def test_delete_ticket_type(self):
        ticket_type_1 = TicketType.objects.create(name="Test Type 1")
        ticket_type_2 = TicketType.objects.create(name="Test Type 1")
        response = self.client.delete(
            "/tickets/api/tickettypes/%d/" % (ticket_type_1.id, ), )
        [ticket_type] = TicketType.objects.all()
        self.assertEqual(response.data, None)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(ticket_type, ticket_type_2)