Merge pull request #264 from taigaio/moving-github-tests-to-contrib-repo

Moving tests from taiga-back to the plugin repository
remotes/origin/enhancement/email-actions
David Barragán Merino 2015-02-24 13:34:37 +01:00
commit 39d2faba80
3 changed files with 0 additions and 314 deletions

View File

@ -26,7 +26,6 @@ MEDIA_ROOT = "/tmp"
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
INSTALLED_APPS = INSTALLED_APPS + [ INSTALLED_APPS = INSTALLED_APPS + [
"tests", "tests",
"taiga_contrib_github_auth",
] ]
REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] = { REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] = {

View File

@ -16,20 +16,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest import pytest
from unittest.mock import patch, Mock
from django.apps import apps
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core import mail from django.core import mail
from .. import factories 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 pytestmark = pytest.mark.django_db
@ -91,140 +83,6 @@ def test_response_200_in_public_registration(client, settings):
assert len(mail.outbox) == 1 assert len(mail.outbox) == 1
assert mail.outbox[0].subject == "You've been Taigatized!" 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): def test_respond_400_if_username_is_invalid(client, settings, register_form):
settings.PUBLIC_REGISTER_ENABLED = True settings.PUBLIC_REGISTER_ENABLED = True

View File

@ -1,171 +0,0 @@
# Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014 Anler Hernández <hello@anler.me>
# 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 <http://www.gnu.org/licenses/>.
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)