class UtilsTestCase(TestCase):

    def setUp(self):
        self.fixtures = GenerateFixtures()

    def tearDown(self):
        self.fixtures.clean_tables()

    @httpretty.activate
    def test_facebook_access_token(self):
        url = '{0}/oauth/access_token?'.format(FACEBOOK.get('graph_url'))

        httpretty.register_uri(httpretty.POST, url,
                               body="'access_token=28252560|CcNsxMwOhoJTe'")

        access_token = get_facebook_access_token()

        self.assertEqual(access_token, "28252560|CcNsxMwOhoJTe'")

    @httpretty.activate
    def test_facebook_user_data(self):
        # config vars
        facebookId = "10011010101"
        access_token = '282525608440728|CcNsxMwOhoJTe'
        url_token = '{0}/oauth/access_token?'.format(FACEBOOK.get('graph_url'))

        httpretty.register_uri(httpretty.POST, url_token,
                               body="'access_token=28252560|CcNsxMwOhoJTe'")

        user_data_url = "{0}/{1}/?&access_token={2}".format(
                     FACEBOOK.get('graph_url'),
                     facebookId,
                     access_token)

        httpretty.register_uri(httpretty.GET, user_data_url,
                               body='{"id":"10011010101","name": "Arthur"}')

        user_data_response = get_facebook_user_data(facebookId)

        self.assertEqual(user_data_response.json().get('id'), facebookId)
Beispiel #2
0
 def setUp(self):
     self.fixtures = GenerateFixtures()
     self.factory = APIRequestFactory()
     self.client = APIClient()
 def setUp(self):
     self.fixtures = GenerateFixtures()
Beispiel #4
0
class ApiTestCase(APITestCase):

    def setUp(self):
        self.fixtures = GenerateFixtures()
        self.factory = APIRequestFactory()
        self.client = APIClient()

    def tearDown(self):
        self.fixtures.clean_tables()

    def test_get_all_users(self):
        # create more Persons
        self.fixtures.fake_person(facebookId=10999901201,
                                  name="Afonso Ramos",
                                  username="******",
                                  gender="male")

        self.fixtures.fake_person(facebookId=10788867657,
                                  name="Alana Monteiro",
                                  username="******",
                                  gender="female")

        response = self.client.get('/api/v1/person/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        assert len(response.data) == 3

    def test_get_partial_users(self):
        # create more Persons
        self.fixtures.fake_person(facebookId=10999901201,
                                  name="Afonso Ramos",
                                  username="******",
                                  gender="male")

        self.fixtures.fake_person(facebookId=10788867657,
                                  name="Alana Monteiro",
                                  username="******",
                                  gender="female")

        response = self.client.get('/api/v1/person/?limit=2')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        assert len(response.data) == 2

    def test_get_user(self):

        facebookId = '101001010101'

        response = self.client.get('/api/v1/person/{}/'.format(facebookId))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(response.data.get('facebookId'), facebookId)

    def test_get_user_not_found(self):

        facebookId = '101001010444'

        response = self.client.get('/api/v1/person/{}/'.format(facebookId))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    @mock.patch('person.utils.get_facebook_access_token')
    @mock.patch('person.serializers.get_facebook_user_data')
    def test_post_valid_facebookId(self,
                                   facebook_user_data_mock,
                                   access_token_mock):

        access_token_mock.return_value = "21ghhgv31hjg32j1h3g12"

        facebook_response = Response()
        facebook_response.status_code = 200
        facebook_response.json = mock.MagicMock(return_value={
                                                "id": "10000076263112",
                                                "name": "Arthur Feitosa"})
        facebook_response.content_type = "aplication/json"

        facebook_user_data_mock.return_value = facebook_response

        data = {'facebookId': '10000076263112'}
        response = self.client.post('/api/v1/person/', data, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        facebook_user_data_mock.assert_called_once_with('10000076263112')

    def test_post_invalid_facebookId(self):
        data = {'facebookId': '10000076263112b5f3'}
        response = self.client.post('/api/v1/person/', data, format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_post_empty_facebookId(self):
        response = self.client.post('/api/v1/person/', {}, format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    @mock.patch('person.utils.get_facebook_access_token')
    @mock.patch('person.serializers.get_facebook_user_data')
    def test_post_user_already_registed(self,
                                        facebook_user_data_mock,
                                        access_token_mock):

        self.fixtures.fake_person(facebookId=10999901201,
                                  name="Afonso Ramos",
                                  username="******",
                                  gender="male")

        access_token_mock.return_value = "21ghhgv31hjg32j1h3g12"
        facebook_response = Response()
        facebook_response.status_code = 200
        facebook_response.json = mock.MagicMock(return_value={
                                                "id": "10999901201",
                                                "name": "afonsoramosrb"})
        facebook_response.content_type = "aplication/json"

        facebook_user_data_mock.return_value = facebook_response

        data = {'facebookId': '10999901201'}

        with transaction.atomic():
            response = self.client.post('/api/v1/person/', data, format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    @mock.patch('person.utils.get_facebook_access_token')
    @mock.patch('person.serializers.get_facebook_user_data')
    def test_post_not_found_facebookId(self,
                                       facebook_user_data_mock,
                                       access_token_mock):
        access_token_mock.return_value = "21ghhgv31hjg32j1h3g12"

        facebook_response = Response()
        facebook_response.status_code = 400
        facebook_response.json = mock.MagicMock(return_value={
                                                "id": "10000076263112",
                                                "name": "Arthur Feitosa"})
        facebook_response.content_type = "aplication/json"

        facebook_user_data_mock.return_value = facebook_response

        data = {'facebookId': '10000076263112'}
        response = self.client.post('/api/v1/person/', data, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        facebook_user_data_mock.assert_called_once_with('10000076263112')

    def test_delete_user(self):

        facebookId = '101001010101'

        response = self.client.delete('/api/v1/person/{}/'.format(facebookId))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)