From c712e1d8e7f8ccb299f11624ec77140be1c087f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 11 Jun 2014 19:20:09 +0200 Subject: [PATCH] Add some tests of the github connector --- tests/integration/test_auth_api.py | 61 ++++++++++++ tests/unit/test_connectors_github.py | 135 +++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tests/unit/test_connectors_github.py diff --git a/tests/integration/test_auth_api.py b/tests/integration/test_auth_api.py index 54dd4bd1..ec11aac6 100644 --- a/tests/integration/test_auth_api.py +++ b/tests/integration/test_auth_api.py @@ -1,8 +1,13 @@ import pytest +from unittest.mock import patch, Mock +from django.db.models.loading import get_model from django.core.urlresolvers import reverse from .. import factories +from taiga.base.connectors import github + + pytestmark = pytest.mark.django_db @@ -40,3 +45,59 @@ def test_respond_201_if_domain_allows_public_registration(client, register_form) response = client.post(reverse("auth-register"), register_form) assert response.status_code == 201 + + +def test_response_200_in_registration_with_github_account(client): + form = {"type": "github", + "code": "xxxxxx"} + + with patch("taiga.base.connectors.github.me") as m_me: + m_me.return_value = ("mmcfly@bttf.com", + github.User(id=1955, + username="mmcfly", + full_name="martin seamus mcfly", + bio="time traveler")) + + response = client.post(reverse("auth-list"), form) + assert response.status_code == 200 + assert response.data["username"] == "mmcfly" + assert response.data["auth_token"] != "" and response.data["auth_token"] != None + assert response.data["email"] == "mmcfly@bttf.com" + assert response.data["full_name"] == "martin seamus mcfly" + assert response.data["bio"] == "time traveler" + assert response.data["github_id"] == 1955 + + +def test_response_200_in_registration_with_github_account_in_a_project(client): + membership_model = get_model("projects", "Membership") + membership = factories.MembershipFactory(user=None) + form = {"type": "github", + "code": "xxxxxx", + "token": membership.token} + + with patch("taiga.base.connectors.github.me") as m_me: + m_me.return_value = ("mmcfly@bttf.com", + github.User(id=1955, + username="mmcfly", + full_name="martin seamus mcfly", + bio="time traveler")) + + response = client.post(reverse("auth-list"), form) + assert response.status_code == 200 + assert membership_model.objects.get(token=form["token"]).user.username == "mmcfly" + + +def test_response_404_in_registration_with_github_account_in_a_project_with_invalid_token(client): + form = {"type": "github", + "code": "xxxxxx", + "token": "123456"} + + with patch("taiga.base.connectors.github.me") as m_me: + m_me.return_value = ("mmcfly@bttf.com", + github.User(id=1955, + username="mmcfly", + full_name="martin seamus mcfly", + bio="time traveler")) + + response = client.post(reverse("auth-list"), form) + assert response.status_code == 404 diff --git a/tests/unit/test_connectors_github.py b/tests/unit/test_connectors_github.py new file mode 100644 index 00000000..47c8a8e0 --- /dev/null +++ b/tests/unit/test_connectors_github.py @@ -0,0 +1,135 @@ +import pytest + +from unittest.mock import patch, Mock +from taiga.base.connectors import github +from taiga.base.connectors import exceptions as exc + + +def test_url_builder(): + assert (github._build_url("login", "authorize") == + "https://api.github.com/login/oauth/authorize") + assert (github._build_url("login","access-token") == + "https://api.github.com/login/oauth/access_token") + assert (github._build_url("user", "profile") == + "https://api.github.com/user") + assert (github._build_url("user", "emails") == + "https://api.github.com/user/emails") + + +def test_login_success(): + with patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.post.return_value = m_response = Mock() + m_response.status_code = 200 + m_response.json.return_value = {"access_token": "xxxxxxxx"} + + auth_info = github.login("*access-code*", "**client-id**", "*client-secret*", github.HEADERS) + + assert auth_info.access_token == "xxxxxxxx" + m_requests.post.assert_called_once_with("https://github.com/login/oauth/access_token", + headers=github.HEADERS, + params={'code': '*access-code*', + 'scope': 'user:emails', + 'client_id': '**client-id**', + 'client_secret': '*client-secret*'}) + + +def test_login_whit_errors(): + with pytest.raises(exc.GitHubApiError) as e, \ + patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.post.return_value = m_response = Mock() + m_response.status_code = 200 + m_response.json.return_value = {"error": "Invalid credentials"} + + auth_info = github.login("*access-code*", "**client-id**", "*ient-secret*", github.HEADERS) + assert e.value.status_code == 400 + assert e.value.detail["status_code"] == 200 + assert e.value.detail["error"] == "Invalid credentials" + + +def test_get_user_profile_success(): + with patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.get.return_value = m_response = Mock() + m_response.status_code = 200 + m_response.json.return_value = {"id": 1955, + "login": "mmcfly", + "name": "martin seamus mcfly", + "bio": "time traveler"} + + user_profile = github.get_user_profile(github.HEADERS) + + assert user_profile.id == 1955 + assert user_profile.username == "mmcfly" + assert user_profile.full_name == "martin seamus mcfly" + assert user_profile.bio == "time traveler" + m_requests.get.assert_called_once_with("https://api.github.com/user", + headers=github.HEADERS) + + +def test_get_user_profile_whit_errors(): + with pytest.raises(exc.GitHubApiError) as e, \ + patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.get.return_value = m_response = Mock() + m_response.status_code = 401 + m_response.json.return_value = {"error": "Invalid credentials"} + + auth_info = github.get_user_profile(github.HEADERS) + assert e.value.status_code == 400 + assert e.value.detail["status_code"] == 401 + assert e.value.detail["error"] == "Invalid credentials" + + +def test_get_user_emails_success(): + with patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.get.return_value = m_response = Mock() + m_response.status_code = 200 + m_response.json.return_value = [{"email": "darth-vader@bttf.com", "primary": False}, + {"email": "mmcfly@bttf.com", "primary": True}] + + emails = github.get_user_emails(github.HEADERS) + + assert len(emails) == 2 + assert emails[0].email == "darth-vader@bttf.com" + assert not emails[0].is_primary + assert emails[1].email == "mmcfly@bttf.com" + assert emails[1].is_primary + m_requests.get.assert_called_once_with("https://api.github.com/user/emails", + headers=github.HEADERS) + + +def test_get_user_emails_whit_errors(): + with pytest.raises(exc.GitHubApiError) as e, \ + patch("taiga.base.connectors.github.requests") as m_requests: + m_requests.get.return_value = m_response = Mock() + m_response.status_code = 401 + m_response.json.return_value = {"error": "Invalid credentials"} + + emails = github.get_user_emails(github.HEADERS) + assert e.value.status_code == 400 + assert e.value.detail["status_code"] == 401 + assert e.value.detail["error"] == "Invalid credentials" + + +def test_me(): + with patch("taiga.base.connectors.github.login") as m_login, \ + patch("taiga.base.connectors.github.get_user_profile") as m_get_user_profile, \ + patch("taiga.base.connectors.github.get_user_emails") as m_get_user_emails: + m_login.return_value = github.AuthInfo(access_token="xxxxxxxx") + m_get_user_profile.return_value = github.User(id=1955, + username="mmcfly", + full_name="martin seamus mcfly", + bio="time traveler") + m_get_user_emails.return_value = [github.Email(email="darth-vader@bttf.com", is_primary=False), + github.Email(email="mmcfly@bttf.com", is_primary=True)] + + email, user = github.me("**access-code**") + + assert email == "mmcfly@bttf.com" + assert user.id == 1955 + assert user.username == "mmcfly" + assert user.full_name == "martin seamus mcfly" + assert user.bio == "time traveler" + + headers = github.HEADERS.copy() + headers["Authorization"] = "token xxxxxxxx" + m_get_user_profile.assert_called_once_with(headers=headers) + m_get_user_emails.assert_called_once_with(headers=headers)