def test_generic_methods(): provider = OAuthProvider(GenericProvider, config) state = "one" auth_url = provider.get_auth_url(state) if not auth_url.find("example.com"): raise AssertionError
def test_get_user(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "account_id": "xb56ksf", "name": "John Doe", "values": [ { "is_primary": True, "type": "email", "email": "*****@*****.**", }, ], } provider = OAuthProvider("bitbucket", config) user = provider.get_user("xxxxxx") if user.get("provider") != "bitbucket": raise AssertionError if user.get("id") != "xb56ksf": raise AssertionError if user.get("email") != "*****@*****.**": raise AssertionError
def test_get_auth_url(): provider = OAuthProvider("google", config) auth_url = provider.set_scopes(["email", "openid"]).get_auth_url() parts = list(parse.urlparse(auth_url)) query = dict(parse.parse_qsl(parts[4])) if query.get("client_id") != "123": raise AssertionError
def test_get_auth_url(): provider = OAuthProvider("microsoft", config) state = "one" auth_url = provider.set_scopes(["email", "openid"]).get_auth_url(state) parts = list(parse.urlparse(auth_url)) query = dict(parse.parse_qsl(parts[4])) if query.get("client_id") != "client_id_1": raise AssertionError
def test_get_token_error(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "error": "Bad verification code" } provider = OAuthProvider("microsoft", config) state = "one" with pytest.raises(BadVerification): provider.get_token("xxxxxx", state)
def test_get_token(mock_requests, token): mock_requests.post.return_value.ok = True mock_requests.post.return_value.json.return_value = token code = "__testcode__" provider = OAuthProvider("google", config) auth_token = provider.get_token(code) if auth_token.get("access_token") != "123": raise AssertionError if auth_token.get("token_type") != "Bearer": raise AssertionError
def test_get_token(mock_requests): mock_requests.post.return_value.ok = True mock_requests.post.return_value.json.return_value = { "access_token": "xb56ksf", } provider = OAuthProvider("bitbucket", config) code = "one" state = "user_state" token = provider.get_token(code, state) if token.get("access_token") != "xb56ksf": raise AssertionError
def test_get_token(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "access_token": "******", "id": "token_id", "expires": "timestamp", } provider = OAuthProvider("facebook", config) state = "one" code = "oauth_code" token = provider.get_token(code, state) if token.get("access_token") != "******": raise AssertionError
def test_get_user_google(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "sub": "103", "name": "John Doe", "email": "*****@*****.**", } provider = OAuthProvider("google", config) user = provider.get_user("xxxxxx") if user.get("provider") != "google": raise AssertionError if user.get("id") != "103": raise AssertionError if user.get("email") != "*****@*****.**": raise AssertionError
def test_get_user(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "id": "xb56ksf", "name": "John Doe", "userPrincipalName": "*****@*****.**", } provider = OAuthProvider("microsoft", config) user = provider.get_user("xxxxxx") if user.get("provider") != "microsoft": raise AssertionError if user.get("id") != "xb56ksf": raise AssertionError if user.get("email") != "*****@*****.**": raise AssertionError
def test_get_user(mock_requests): mock_requests.get.return_value.ok = True mock_requests.get.return_value.json.return_value = { "id": "103", "name": "John Doe", "login": "******", "email": None, } provider = OAuthProvider("github", config) user = provider.get_user("xxxxxx") if user.get("provider") != "github": raise AssertionError if user.get("id") != "103": raise AssertionError if user.get("username") != "johndoe": raise AssertionError
def test_set_scopes(): with pytest.raises(ValueError): provider = OAuthProvider("invalid", config) provider.set_scopes(["error"])