From f30b8f845154d4fee4ceb3b8787a98ff85f0b8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 24 Feb 2015 13:10:40 +0100 Subject: [PATCH] Moving tests from taiga-back to the plugin repository --- settings/testing.py | 1 - tests/integration/test_auth_api.py | 142 ---------------------- tests/unit/test_connectors_github.py | 171 --------------------------- 3 files changed, 314 deletions(-) delete mode 100644 tests/unit/test_connectors_github.py diff --git a/settings/testing.py b/settings/testing.py index f32cb405..9fb6ec74 100644 --- a/settings/testing.py +++ b/settings/testing.py @@ -26,7 +26,6 @@ MEDIA_ROOT = "/tmp" EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" INSTALLED_APPS = INSTALLED_APPS + [ "tests", - "taiga_contrib_github_auth", ] REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] = { diff --git a/tests/integration/test_auth_api.py b/tests/integration/test_auth_api.py index d7d0261c..e776b569 100644 --- a/tests/integration/test_auth_api.py +++ b/tests/integration/test_auth_api.py @@ -16,20 +16,12 @@ # along with this program. If not, see . import pytest -from unittest.mock import patch, Mock -from django.apps import apps from django.core.urlresolvers import reverse from django.core import mail from .. import factories -from taiga.front import resolve as resolve_front_url -from taiga.users import models -from taiga.auth.tokens import get_token_for_user - -from taiga_contrib_github_auth import connector as github_connector - pytestmark = pytest.mark.django_db @@ -91,140 +83,6 @@ def test_response_200_in_public_registration(client, settings): assert len(mail.outbox) == 1 assert mail.outbox[0].subject == "You've been Taigatized!" -def test_response_200_in_registration_with_github_account(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - form = {"type": "github", - "code": "xxxxxx"} - - auth_data_model = apps.get_model("users", "AuthData") - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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 auth_data_model.objects.filter(user__username="mmcfly", key="github", value="1955").count() == 1 - -def test_response_200_in_registration_with_github_account_and_existed_user_by_email(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - form = {"type": "github", - "code": "xxxxxx"} - user = factories.UserFactory() - user.email = "mmcfly@bttf.com" - user.save() - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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"] == user.username - assert response.data["auth_token"] != "" and response.data["auth_token"] != None - assert response.data["email"] == user.email - assert response.data["full_name"] == user.full_name - assert response.data["bio"] == user.bio - assert user.auth_data.filter(key="github", value="1955").count() == 1 - -def test_response_200_in_registration_with_github_account_and_existed_user_by_github_id(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - form = {"type": "github", - "code": "xxxxxx"} - user = factories.UserFactory.create() - - auth_data_model = apps.get_model("users", "AuthData") - auth_data_model.objects.create(user=user, key="github", value="1955", extra={}) - - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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" - -def test_response_200_in_registration_with_github_account_and_change_github_username(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - form = {"type": "github", - "code": "xxxxxx"} - user = factories.UserFactory() - user.username = "mmcfly" - user.save() - - auth_data_model = apps.get_model("users", "AuthData") - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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-1" - 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 auth_data_model.objects.filter(user__username="mmcfly-1", key="github", value="1955").count() == 1 - -def test_response_200_in_registration_with_github_account_in_a_project(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - membership_model = apps.get_model("projects", "Membership") - membership = factories.MembershipFactory(user=None) - form = {"type": "github", - "code": "xxxxxx", - "token": membership.token} - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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_in_a_project_with_invalid_token(client, settings): - settings.PUBLIC_REGISTER_ENABLED = False - form = {"type": "github", - "code": "xxxxxx", - "token": "123456"} - - with patch("taiga_contrib_github_auth.connector.me") as m_me: - m_me.return_value = ("mmcfly@bttf.com", - github_connector.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 - def test_respond_400_if_username_is_invalid(client, settings, register_form): settings.PUBLIC_REGISTER_ENABLED = True diff --git a/tests/unit/test_connectors_github.py b/tests/unit/test_connectors_github.py deleted file mode 100644 index abf35d7f..00000000 --- a/tests/unit/test_connectors_github.py +++ /dev/null @@ -1,171 +0,0 @@ -# Copyright (C) 2014 Andrey Antukh -# Copyright (C) 2014 Jesús Espino -# Copyright (C) 2014 David Barragán -# Copyright (C) 2014 Anler Hernández -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -import pytest - -from unittest.mock import patch, Mock -from taiga_contrib_github_auth import connector as github - - -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_without_settings_params(): - with pytest.raises(github.GitHubApiError) as e, \ - patch("taiga_contrib_github_auth.connector.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**", "*ient-secret*", github.HEADERS) - assert e.value.status_code == 400 - assert "error_message" in e.value.detail - - -def test_login_success(): - with patch("taiga_contrib_github_auth.connector.requests") as m_requests, \ - patch("taiga_contrib_github_auth.connector.CLIENT_ID") as CLIENT_ID, \ - patch("taiga_contrib_github_auth.connector.CLIENT_SECRET") as CLIENT_SECRET: - CLIENT_ID = "*CLIENT_ID*" - CLIENT_SECRET = "*CLIENT_SECRET*" - 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(github.GitHubApiError) as e, \ - patch("taiga_contrib_github_auth.connector.requests") as m_requests, \ - patch("taiga_contrib_github_auth.connector.CLIENT_ID") as CLIENT_ID, \ - patch("taiga_contrib_github_auth.connector.CLIENT_SECRET") as CLIENT_SECRET: - CLIENT_ID = "*CLIENT_ID*" - CLIENT_SECRET = "*CLIENT_SECRET*" - 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_contrib_github_auth.connector.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(github.GitHubApiError) as e, \ - patch("taiga_contrib_github_auth.connector.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_contrib_github_auth.connector.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(github.GitHubApiError) as e, \ - patch("taiga_contrib_github_auth.connector.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_contrib_github_auth.connector.login") as m_login, \ - patch("taiga_contrib_github_auth.connector.get_user_profile") as m_get_user_profile, \ - patch("taiga_contrib_github_auth.connector.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)