def test_oauth_login(self, http_requests): g = Google(http_requests()) g.init(**self.mock_options) result = g.oauth_login(_MockFlaskRequest()) assert result["access_token"] == "<access_token>" # TODO better tests g.oauth_login(_MockFlaskRequest(), redirect_uri="<new_redirect_uri>") g.update_base_path("<url>", "<new_redirect_uri>") assert "<new_redirect_uri>" in g._url
def test_oauth_login_no_code_raises(self, http_requests): g = Google(http_requests(None)) g.init(**self.mock_options) request = _MockFlaskRequest() def get_json(): return {} setattr(request, "get_json", get_json) with pytest.raises(ClientExchangeCodeError): _ = g.oauth_login(request)
def test_google(self, http_requests): g = Google(http_requests()) g.init(**self.mock_options) assert hasattr(g, "client_id") assert hasattr(g, "client_secret") assert hasattr(g, "grant_type") assert hasattr(g, "redirect_uri") assert hasattr(g, "tablename") assert hasattr(g, "email_field") assert hasattr(g, "expires_in")
def test_tear_down(self, http_requests, MockAOuthModel): mock_user = MockAOuthModel(email="*****@*****.**") g = GoogleTestUtil(http_requests(GOOGLE_OAUTH_URL)) g.init(**self.mock_options) result = g.create_test_headers(email="*****@*****.**", entity=mock_user, scope="application") assert result == {'X-Auth-Token': 'Bearer [email protected]'} assert g.test_metadata["*****@*****.**"]["email"] == "*****@*****.**" assert g.test_metadata["*****@*****.**"]["entity"] == mock_user assert g.test_metadata["*****@*****.**"]["scope"] is "application" g.tear_down() assert g.test_metadata["*****@*****.**"]["email"] == "*****@*****.**" g.tear_down(scope="application") assert g.test_metadata == {}
def test_update_base_path(self, http_requests): g = Google(http_requests()) g.init(**self.mock_options) g.code = "<CODE>" g._url = "https://oauth2.googleapis.com/token" g.update_base_path(g._url, "http://localhost:3000") expected = "https://oauth2.googleapis.com/token?" \ "code=<CODE>&" \ "client_id=<CLIENT_ID>&" \ "client_secret=<CLIENT_SECRET>&" \ "redirect_uri=http://localhost:3000&" \ "grant_type=authorization_code&" \ "expires_in=3600" assert g._url == expected
def test_authorize(self, http_requests): """ { "family_name": "", "name": "", "picture": "", "locale": "en", "email": "", "given_name": "Joe", "id": "", "verified_email": true } """ g = GoogleTestUtil(http_requests(GOOGLE_OAUTH_URL)) g.init(**self.mock_options) token = "" result = g.authorize(token) assert "email" in result
def test_oauth_login_raises(self, http_requests): g = Google(http_requests()) g.init(**self.mock_options) with pytest.raises(RequestAttributeError): _ = g.oauth_login(None)
def test_before_middleware(self, monkeypatch, TestMockEntity, MockAOuthModel, mock_token, http_requests): app = Flask(__name__) @app.route("/test", methods=["GET"]) def fc_one(): return "/test" # Manually set the primary key entity = TestMockEntity(id=1, user_name="joe") oauth_entity = MockAOuthModel(id=1, email="*****@*****.**") after_oauth = TestMockEntity(id=1, user_name="terry") ctx = app.test_request_context("/test") ctx.push() assert entity.user_name == "joe" assert entity.id == 1 assert oauth_entity.id == 1 assert oauth_entity.email == "*****@*****.**" assert after_oauth.user_name == "terry" assert after_oauth.id == 1 config = Config() config.init_config(self.app_config, google_oauth=self.oauth_options) config.entity_models = [TestMockEntity, MockAOuthModel] entity = Entity(config) google = Google(http_requests(oauth_urls)) google.init(**config.google_oauth) routing = Routing() routing.init(app, config, entity, google) with ctx: # token from args monkeypatch.setattr("flask.request.args", MockArgs(mock_token)) entity.clean_up() assert routing.entity.entity_key == None assert routing.entity.tablename == None routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from OAuth headers - X-Auth-Token monkeypatch.setattr("flask.request.args", {}) monkeypatch.setattr("flask.request.headers", MockArgs(mock_token, True)) entity.clean_up() assert routing.entity.oauth_entity_key == None assert routing.entity.tablename == None # routing.before_middleware() # assert ctx.g.oauth_tablename == [(1, '*****@*****.**')] # with ctx: # # token from oauth headers # monkeypatch.setattr("flask.request.args", {}) # monkeypatch.setattr("flask.request.headers", MockArgs("<access_token>", "X-Auth-Token")) # entity.clean_up() # assert routing.entity.entity_key == None # assert routing.entity.oauth_entity_key == None # assert routing.entity.tablename == None # routing.before_middleware() # assert ctx.g.oauth_tablename == [(1, "*****@*****.**")] # Fixes bug - "entity key state gets stale between requests #171" # https://github.com/joegasewicz/flask-jwt-router/issues/171 with ctx: monkeypatch.setattr("flask.request.headers", MockArgs("<after_token>", "Authorization")) entity.clean_up() assert routing.entity.entity_key == None assert routing.entity.oauth_entity_key == None assert routing.entity.tablename == None