Skip to content

Commit

Permalink
fix kiwitcms#1620 add automated tests for missing coverage in tcms.rp…
Browse files Browse the repository at this point in the history
…c.api.auth
  • Loading branch information
iamabhishek0 committed Nov 5, 2020
1 parent ac31b26 commit bbc7b65
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tcms/rpc/tests/test_auth.py
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())

0 comments on commit bbc7b65

Please sign in to comment.