Esempio n. 1
0
    def test_contact_put(self):
        contact = Contact.objects.get(email=self.user.email)

        test_data = {
            "id": contact.id,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact
        request = self.factory.put('api/contact', test_data)
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 200
        self.assertEquals(
            response.status_code,
            status.HTTP_200_OK,
            response.data)
        # Results match tests data
        self.assertEquals(response.data, test_data, response.data)
Esempio n. 2
0
    def test_contact_put_email_validation(self):
        contact = Contact.objects.get(email=self.user.email)

        test_data = {
            "id": contact.id,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",  # existing email from setUp
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact
        request = self.factory.put('api/contact', test_data)
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 400
        self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST,
                          response.data)
        # Expected error message
        self.assertEqual(str(response.data.get('email')[0]),
                         'This field must be unique.', response.data)
Esempio n. 3
0
    def test_contact_post_email_validation(self):
        test_data = {
            "id": 5,
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": self.user.email,  # existing email from setUp
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        request.user = self.user
        response = view(request)

        # HTTP 400
        self.assertEqual(
            response.status_code,
            status.HTTP_400_BAD_REQUEST,
            response.data)
        # Expected error message
        self.assertEqual(str(response.data.get('email')[
                         0]), 'This field must be unique.', response.data)
Esempio n. 4
0
    def test_contact_put_email_validation(self):
        contact = Contact.objects.get(email=self.user.email)

        test_data = {
            "id": contact.id,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",  # existing email from setUp
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact
        request = self.factory.put('api/contact', test_data)
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 400
        self.assertEquals(
            response.status_code,
            status.HTTP_400_BAD_REQUEST,
            response.data)
        # Expected error message
        self.assertEqual(str(response.data.get('email')[
                         0]), 'This field must be unique.', response.data)
Esempio n. 5
0
    def test_contact_create(self):
        test_data = {
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        request.user = self.user
        response = view(request)

        # HTTP 201
        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
            response.data)
        # remove the 'id' element from results to match the tests data json
        del response.data['id']
        # Results match tests data
        self.assertEqual(response.data, test_data, response.data)
Esempio n. 6
0
    def test_contact_list_auth(self):
        view = ContactViewSet.as_view({'get': 'list'})
        # GET contact where id == 1
        request = self.factory.get('api/contact')

        response = view(request)

        # HTTP 401
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED,
                         response.data)
Esempio n. 7
0
    def test_contact_list_auth(self):
        view = ContactViewSet.as_view({'get': 'list'})
        # GET contact where id == 1
        request = self.factory.get('api/contact')

        response = view(request)

        # HTTP 401
        self.assertEqual(
            response.status_code,
            status.HTTP_401_UNAUTHORIZED,
            response.data)
Esempio n. 8
0
    def test_contact_patch_auth(self):
        view = ContactViewSet.as_view({
            'get': 'retrieve',
            'patch': 'partial_update'
        })
        # update email with an existing email
        request = self.factory.patch('api/contact/1',
                                     {'email': '*****@*****.**'})
        response = view(request, pk="1")

        # HTTP 401
        self.assertEquals(response.status_code, status.HTTP_401_UNAUTHORIZED,
                          response.data)
Esempio n. 9
0
    def test_contact_patch_auth(self):
        view = ContactViewSet.as_view(
            {'get': 'retrieve', 'patch': 'partial_update'})
        # update email with an existing email
        request = self.factory.patch(
            'api/contact/1', {'email': '*****@*****.**'})
        response = view(request, pk="1")

        # HTTP 401
        self.assertEquals(
            response.status_code,
            status.HTTP_401_UNAUTHORIZED,
            response.data)
Esempio n. 10
0
    def test_contact_get(self):
        # contact to retrieve from GET
        contact = Contact.objects.get(email="*****@*****.**")

        view = ContactViewSet.as_view({'get': 'retrieve'})
        # GET contact using contact.id
        request = self.factory.get('api/contact')
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 200
        self.assertEqual(response.status_code, status.HTTP_200_OK,
                         response.data)
        # Results match tests data
        self.assertEqual(response.data, model_to_dict(contact), response.data)
Esempio n. 11
0
    def test_contact_patch_fail(self):
        contact = Contact.objects.get(email=self.user.email)

        view = ContactViewSet.as_view(
            {'get': 'retrieve', 'patch': 'partial_update'})
        # update email
        request = self.factory.patch(
            'api/contact', {"email": "*****@*****.**"})
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 405
        self.assertEquals(
            response.status_code,
            status.HTTP_405_METHOD_NOT_ALLOWED,
            response.data)
Esempio n. 12
0
    def test_contact_get(self):
        # contact to retrieve from GET
        contact = Contact.objects.get(email="*****@*****.**")

        view = ContactViewSet.as_view({'get': 'retrieve'})
        # GET contact using contact.id
        request = self.factory.get('api/contact')
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 200
        self.assertEqual(
            response.status_code,
            status.HTTP_200_OK,
            response.data)
        # Results match tests data
        self.assertEqual(response.data, model_to_dict(contact), response.data)
Esempio n. 13
0
    def test_contact_patch_validation(self):
        contact = Contact.objects.get(email=self.user.email)

        view = ContactViewSet.as_view({
            'get': 'retrieve',
            'patch': 'partial_update'
        })
        # update email with an existing email
        request = self.factory.patch('api/contact',
                                     {'email': '*****@*****.**'})
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 400
        self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST,
                          response.data)
        # Expected error message
        self.assertEquals(str(response.data.get("email")[0]),
                          'This field must be unique.', response.data)
Esempio n. 14
0
    def test_contact_patch(self):
        contact = Contact.objects.get(email=self.user.email)

        view = ContactViewSet.as_view({
            'get': 'retrieve',
            'patch': 'partial_update'
        })
        # update email
        request = self.factory.patch('api/contact',
                                     {"email": "*****@*****.**"})
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 200
        self.assertEquals(response.status_code, status.HTTP_200_OK,
                          response.data)
        # Email has been updated
        self.assertEquals(response.data.get("email"), '*****@*****.**',
                          response.data)
Esempio n. 15
0
    def test_contact_create_auth(self):
        test_data = {
            "id": 5,
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        response = view(request)

        # HTTP 401
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED,
                         response.data)
Esempio n. 16
0
    def test_contact_put_auth(self):
        test_data = {
            "id": 1,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact where id == 1 with test_data
        request = self.factory.put('api/contact/1', test_data)
        response = view(request, pk="1")

        # HTTP 401
        self.assertEquals(response.status_code, status.HTTP_401_UNAUTHORIZED,
                          response.data)
Esempio n. 17
0
    def test_contact_put_auth(self):
        test_data = {
            "id": 1,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact where id == 1 with test_data
        request = self.factory.put('api/contact/1', test_data)
        response = view(request, pk="1")

        # HTTP 401
        self.assertEquals(
            response.status_code,
            status.HTTP_401_UNAUTHORIZED,
            response.data)
Esempio n. 18
0
    def test_contact_create_auth(self):
        test_data = {
            "id": 5,
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        response = view(request)

        # HTTP 401
        self.assertEqual(
            response.status_code,
            status.HTTP_401_UNAUTHORIZED,
            response.data)
Esempio n. 19
0
    def test_contact_create(self):
        test_data = {
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        request.user = self.user
        response = view(request)

        # HTTP 201
        self.assertEqual(response.status_code, status.HTTP_201_CREATED,
                         response.data)
        # remove the 'id' element from results to match the tests data json
        del response.data['id']
        # Results match tests data
        self.assertEqual(response.data, test_data, response.data)
Esempio n. 20
0
    def test_contact_post_email_validation(self):
        test_data = {
            "id": 5,
            "title": "Mr.",
            "given_name": "Test",
            "surname": "Test",
            "email": self.user.email,  # existing email from setUp
            "phone": "123456789",
            "organisation": "Monash"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'post': 'create'})
        # POST create new contact using tests data
        request = self.factory.post('api/contact', test_data)
        request.user = self.user
        response = view(request)

        # HTTP 400
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST,
                         response.data)
        # Expected error message
        self.assertEqual(str(response.data.get('email')[0]),
                         'This field must be unique.', response.data)
Esempio n. 21
0
    def test_contact_put(self):
        contact = Contact.objects.get(email=self.user.email)

        test_data = {
            "id": contact.id,
            "title": "Mr",
            "given_name": "Test",
            "surname": "MeRC",
            "email": "*****@*****.**",
            "phone": "123456789",
            "organisation": "Google"
        }

        view = ContactViewSet.as_view({'get': 'retrieve', 'put': 'update'})
        # update contact
        request = self.factory.put('api/contact', test_data)
        request.user = self.user
        response = view(request, pk=contact.id)

        # HTTP 200
        self.assertEquals(response.status_code, status.HTTP_200_OK,
                          response.data)
        # Results match tests data
        self.assertEquals(response.data, test_data, response.data)