def test_can_send_signal_on_post_authentication(self): test_result = {} @signals.authorized_user.connect def test_signal(provider, user_data=None, handler=None): test_result['provider'] = 'google' expect(provider).to_equal('google') expect(user_data).to_equal({ "authenticated": True, "provider": "google", "email": "*****@*****.**", "name": "Teste", "id": "56789" }) user_data['something'] = 'else' with patch.object(GoogleProvider, '_fetch_userinfo') as provider_mock: result = gen.Future() response_mock = Mock(code=200, body=( '{"email":"*****@*****.**", "name":"Teste", "id":"56789"}' )) result.set_result(response_mock) provider_mock.return_value = result response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': 'VALID-TOKEN', 'provider': 'google' }) ) expect(response.code).to_equal(200) data = utils.loads(response.body) expect(data['authenticated']).to_be_true() expect(data['something']).to_equal('else') expect(test_result).to_include('provider')
def test_can_authenticate(self): response = yield self.http_client.fetch( self.get_url('/authenticate/mock/'), method='POST', body=utils.dumps(dict(access_token='1234567890')) ) expect(response.code).to_equal(200) expect(load_json(response.body)).to_equal(dict(authenticated=True)) expect(self.server.handlers_cfg['cookie_name'] in response.headers.get('Set-Cookie')).to_equal(True)
def test_cannot_authenticate_a_user_with_invalid_google_plus_token(self): try: response = yield self.http_client.fetch( self.get_url('/authenticate/google/'), method='POST', body=utils.dumps({ 'access_token': 'INVALID-TOKEN', }) ) except HTTPError as e: response = e.response expect(response.code).to_equal(401) expect(response.reason).to_equal('Unauthorized')
def test_can_authenticate(self): response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': '1234567890', 'provider': 'mock' }) ) expect(response.code).to_equal(200) expect(load_json(response.body)).to_be_like(dict(authenticated=True, id="123")) cookie_name = self.server.application.authentication_options['cookie_name'] expect(cookie_name in response.headers.get('Set-Cookie')).to_equal(True)
def test_cant_authenticate(self): try: yield self.http_client.fetch( self.get_url('/authenticate/mock2/'), method='POST', body=utils.dumps(dict(access_token='1234567890')) ) except HTTPError as e: expect(e.response.code).to_equal(401) expect(e.response.reason).to_equal('Unauthorized') expect(e.response.headers.get('Cookie')).to_equal(None) expect(e.response.headers.get('Set-Cookie')).to_equal(None) else: assert False, 'Should not get this far'
def test_cant_authenticate_with_invalid_provider(self): try: response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': '1234567890', 'provider': 'invalid' }) ) except HTTPError as e: response = e.response expect(response.code).to_equal(401) expect(response.reason).to_equal('Unauthorized')
def test_cannot_authenticate_a_user_with_invalid_google_plus_token(self): with patch.object(GoogleProvider, '_fetch_userinfo') as provider_mock: result = gen.Future() response_mock = Mock(code=401, reason='Unauthorized') result.set_result(response_mock) provider_mock.return_value = result try: response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': 'INVALID-TOKEN', 'provider': 'google' }) ) except HTTPError as e: response = e.response expect(response.code).to_equal(401) expect(response.reason).to_equal('Unauthorized')
def test_can_authenticate_a_user_with_valid_google_plus_token(self): with patch.object(GoogleProvider, '_fetch_userinfo') as provider_mock: result = gen.Future() response_mock = Mock(code=200, body=( '{"email":"*****@*****.**", "name":"Teste", "id":"56789"}' )) result.set_result(response_mock) provider_mock.return_value = result try: response = yield self.http_client.fetch( self.get_url('/authenticate/google/'), method='POST', body=utils.dumps({ 'access_token': 'VALID-TOKEN', }) ) except HTTPError as e: response = e.response expect(response.code).to_equal(200) expect(utils.loads(response.body)['authenticated']).to_be_true()
def test_can_fetch_userinfo_with_proxy_credentials(self): with patch.object(GoogleProvider, '_fetch_userinfo') as provider_mock: result = gen.Future() response_mock = Mock(code=200, body=( '{"email":"*****@*****.**", "name":"teste", "id":"56789"}' )) result.set_result(response_mock) provider_mock.return_value = result try: response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': 'VALID-TOKEN', 'provider': 'google' }) ) except HTTPError as e: response = e.response expect(provider_mock.call_args[0][1]).to_equal( {'proxy_port': '666', 'proxy_username': '******', 'proxy_password': '******', 'proxy_host': '10.10.10.10'} ) expect(response.code).to_equal(200) expect(utils.loads(response.body)['authenticated']).to_be_true()
def test_can_send_signal_on_post_unauthorized(self): test_result = {} @signals.unauthorized_user.connect def test_signal(provider, handler=None): test_result['provider'] = provider with patch.object(GoogleProvider, '_fetch_userinfo') as provider_mock: result = gen.Future() response_mock = Mock(code=401, reason='Unauthorized') result.set_result(response_mock) provider_mock.return_value = result try: response = yield self.http_client.fetch( self.get_url('/auth/signin/'), method='POST', body=utils.dumps({ 'access_token': 'VALID-TOKEN', 'provider': 'google' }) ) except HTTPError as e: response = e.response expect(response.code).to_equal(401) expect(test_result).to_include('provider')
def write_json(self, obj): self.set_header("Content-Type", "application/json") self.write(utils.dumps(obj))
def dump_object(self, instance): return utils.dumps(instance)