taiga-back/tests/integration/test_auth_api.py

109 lines
3.8 KiB
Python

# 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 django.core.urlresolvers import reverse
from django.core import mail
from .. import factories
pytestmark = pytest.mark.django_db
@pytest.fixture
def register_form():
return {"username": "username",
"password": "password",
"full_name": "fname",
"email": "user@email.com",
"type": "public"}
def test_respond_201_when_public_registration_is_enabled(client, settings, register_form):
settings.PUBLIC_REGISTER_ENABLED = True
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 201
def test_respond_400_when_public_registration_is_disabled(client, register_form, settings):
settings.PUBLIC_REGISTER_ENABLED = False
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 400
def test_respond_201_with_invitation_without_public_registration(client, register_form, settings):
settings.PUBLIC_REGISTER_ENABLED = False
user = factories.UserFactory()
membership = factories.MembershipFactory(user=user)
register_form.update({
"type": "private",
"existing": "1",
"token": membership.token,
"username": user.username,
"email": user.email,
"password": user.username,
})
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 201, response.data
def test_response_200_in_public_registration(client, settings):
settings.PUBLIC_REGISTER_ENABLED = True
form = {
"type": "public",
"username": "mmcfly",
"full_name": "martin seamus mcfly",
"email": "mmcfly@bttf.com",
"password": "password",
}
response = client.post(reverse("auth-register"), form)
assert response.status_code == 201
assert response.data["username"] == "mmcfly"
assert response.data["email"] == "mmcfly@bttf.com"
assert response.data["full_name"] == "martin seamus mcfly"
assert len(mail.outbox) == 1
assert mail.outbox[0].subject == "You've been Taigatized!"
def test_respond_400_if_username_is_invalid(client, settings, register_form):
settings.PUBLIC_REGISTER_ENABLED = True
register_form.update({"username": "User Examp:/e"})
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 400
register_form.update({"username": 300*"a"})
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 400
def test_respond_400_if_username_or_email_is_duplicate(client, settings, register_form):
settings.PUBLIC_REGISTER_ENABLED = True
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 201
register_form["username"] = "username"
register_form["email"] = "ff@dd.com"
response = client.post(reverse("auth-register"), register_form)
assert response.status_code == 400