forked from kiwitcms/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix kiwitcms#1620 add automated tests for missing coverage in tcms.rp…
…c.api.auth
- Loading branch information
1 parent
ac31b26
commit bbc7b65
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# pylint: disable=attribute-defined-outside-init | ||
|
||
from xmlrpc.client import Fault as XmlRPCFault | ||
from django.contrib.sessions.models import Session | ||
from tcms.rpc.api.user import User | ||
from tcms.rpc.tests.utils import APITestCase | ||
from tcms.tests import UserFactory | ||
|
||
|
||
class TestAuthLogin(APITestCase): | ||
"""Test Auth.login method""" | ||
|
||
def _fixture_setup(self): | ||
super()._fixture_setup() | ||
# | ||
self.rpc_client.Auth.logout() | ||
self.tester = UserFactory() | ||
self.password = "password" | ||
self.tester.set_password(self.password) | ||
self.tester.save() | ||
|
||
def test_login_with_username(self): | ||
self.rpc_client.Auth.logout() | ||
sesson_id = self.rpc_client.Auth.login(self.tester.username, self.password) | ||
session = Session.objects.get(session_key=sesson_id).get_decoded() | ||
|
||
self.assertIsNotNone(sesson_id) | ||
self.assertEqual(User.objects.get(pk=session.get("_auth_user_id")), self.tester) | ||
|
||
def test_login_without_username(self): | ||
self.rpc_client.Auth.logout() | ||
with self.assertRaisesRegex(XmlRPCFault, "Username and password is required"): | ||
self.rpc_client.Auth.login("", self.password) | ||
|
||
def test_login_without_password(self): | ||
self.rpc_client.Auth.logout() | ||
with self.assertRaisesRegex(XmlRPCFault, "Username and password is required"): | ||
self.rpc_client.Auth.login(self.tester.username, "") | ||
|
||
def test_login_with_incorrect_username(self): | ||
self.rpc_client.Auth.logout() | ||
with self.assertRaisesRegex(XmlRPCFault, "Wrong username or password"): | ||
self.rpc_client.Auth.login("kiwi_tester", self.password) | ||
|
||
def test_login_with_incorrect_password(self): | ||
self.rpc_client.Auth.logout() | ||
with self.assertRaisesRegex(XmlRPCFault, "Wrong username or password"): | ||
self.rpc_client.Auth.login(self.tester.username, "kiwi-password") | ||
|
||
|
||
class TestAuthLogout(APITestCase): | ||
"""Test Auth.logout method""" | ||
|
||
def _fixture_setup(self): | ||
super()._fixture_setup() | ||
|
||
self.rpc_client.Auth.logout() | ||
self.tester = UserFactory() | ||
self.password = "password" | ||
self.tester.set_password(self.password) | ||
self.tester.save() | ||
|
||
def test_logout(self): | ||
sesson_id = self.rpc_client.Auth.login(self.tester.username, self.password) | ||
self.assertIsNotNone(sesson_id) | ||
self.assertTrue(Session.objects.filter(session_key=sesson_id).exists()) | ||
|
||
self.rpc_client.Auth.logout() | ||
self.assertFalse(Session.objects.filter(session_key=sesson_id).exists()) |