def test_success(self):
        username = '******'

        # mock the response, return token
        expected_token = generate_tokens()
        expected_user_data = {
            'pk': 1,
            'first_name': 'My First Name',
            'last_name': 'My last name',
        }

        responses.add(
            responses.POST,
            urljoin(settings.API_URL, 'oauth2/token'),
            body=json.dumps(expected_token),
            status=200,
            content_type='application/json'
        )

        responses.add(
            responses.GET,
            urljoin(settings.API_URL, 'users/', username),
            body=json.dumps(expected_user_data),
            status=200,
            content_type='application/json'
        )

        # authenticate, should return authentication data
        data = api_client.authenticate(username, 'my-password')

        self.assertEqual(data['pk'], expected_user_data.get('pk'))
        self.assertDictEqual(data['token'], expected_token)
        self.assertDictEqual(data['user_data'], expected_user_data)
    def test_base_api_url_allows_trailing_slash(self):
        actual_api_url = settings.API_URL
        # set API_URL and then reload the client to generate
        # a new REQUEST_TOKEN_URL
        settings.API_URL = actual_api_url + '/'
        reload(api_client)

        username = '******'

        # mock the response, return token
        expected_token = generate_tokens()
        expected_user_data = {
            'pk': 1,
            'first_name': 'My First Name',
            'last_name': 'My last name',
        }

        responses.add(
            responses.POST,
            urljoin(actual_api_url, 'oauth2/token/'),
            body=json.dumps(expected_token),
            status=200,
            content_type='application/json'
        )

        responses.add(
            responses.GET,
            urljoin(actual_api_url, 'users/', username),
            body=json.dumps(expected_user_data),
            status=200,
            content_type='application/json'
        )

        # authenticate, should complete without error
        api_client.authenticate(username, 'my-password')

        # revert changes
        settings.API_URL = actual_api_url
        reload(api_client)
    def setUp(self):
        """
        Sets up a request mock object with
        request.user.token == generated token.

        It also defines the {base_url}/test/ endpoint which will be
        used by all the test methods.
        """
        super(GetConnectionTestCase, self).setUp()
        self.request = mock.MagicMock(
            user=mock.MagicMock(
                token=generate_tokens()
            )
        )

        self.test_endpoint = urljoin(settings.API_URL, 'test')