Skip to content

Commit

Permalink
[IMPROVEMENT] Added More Unit tests (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
T1duS authored and canihavesomecoffee committed Nov 22, 2018
1 parent 8555bf8 commit a908ec0
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 9 deletions.
3 changes: 2 additions & 1 deletion mod_auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class User(Base):
password = Column(String(255), unique=False, nullable=False)
role = Column(Role.db_type())

def __init__(self, name, role=Role.user, email=None, password=''):
def __init__(self, name, role=Role.user, email=None, password='', github_token=None):
"""
Parametrized constructor for the User model
Expand All @@ -50,6 +50,7 @@ def __init__(self, name, role=Role.user, email=None, password=''):
self.email = email
self.password = password
self.role = role
self.github_token = github_token

def __repr__(self):
"""
Expand Down
10 changes: 6 additions & 4 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ def load_config(file):
}


def MockRequests(url, data=None):
def MockRequests(url, data=None, timeout=None):
if url == "https://api.github.com/repos/test/test_repo/commits/abcdef":
return MockResponse({}, 200)
return MockResponse({}, 200)
elif url == "https://api.github.com/user":
return MockResponse({"login": "test"}, 200)
elif "https://api.github.com/user" in url:
return MockResponse({"login": url.split("/")[-1]}, 200)
elif url == "https://api.github.com/repos/test_owner/test_repo/issues":
return MockResponse({'number': 1,
'title': 'test title',
Expand Down Expand Up @@ -244,13 +246,13 @@ def complete_forktest(self, test_id, regression_tests):
g.db.add_all(test_result_files)
g.db.commit()

def create_user_with_role(self, user, email, password, role):
def create_user_with_role(self, user, email, password, role, github_token=None):
"""
Create a user with specified user details and role.
"""
from flask import g
user = User(self.user.name, email=self.user.email,
password=User.generate_hash(self.user.password), role=role)
password=User.generate_hash(self.user.password), role=role, github_token=github_token)
g.db.add(user)
g.db.commit()

Expand Down
82 changes: 81 additions & 1 deletion tests/test_auth/TestControllers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time

from tests.base import BaseTestCase, signup_information
from flask import url_for
from tests.base import BaseTestCase, signup_information
from mod_auth.models import Role, User
from mod_auth.controllers import generate_hmac_hash, github_token_validity


Expand Down Expand Up @@ -117,3 +118,82 @@ def test_github_token_validity(self):
"""
res = github_token_validity('token')
self.assertEqual(res, False)

class ManageAccount(BaseTestCase):

def test_edit_username(self):
"""
Test editing user's name.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/account/manage', data=dict(
current_password=self.user.password,
name="T1duS",
email=self.user.email
))
user = User.query.filter(User.name == "T1duS").first()
self.assertNotEqual(user, None)
self.assertIn("Settings saved", str(response.data))

def test_edit_email(self):
"""
Test editing user's email address.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/account/manage', data=dict(
current_password=self.user.password,
name=self.user.name,
email="valid@gmail.com"
))
user = User.query.filter(User.email == "valid@gmail.com").first()
self.assertNotEqual(user, None)
self.assertIn("Settings saved", str(response.data))

def test_edit_invalid_email(self):
"""
Test editing user's email with invalid email address.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/account/manage', data=dict(
current_password=self.user.password,
name=self.user.name,
email="invalid@gg"
))
user = User.query.filter(User.email == "invalid@gg").first()
self.assertEqual(user, None)
self.assertNotIn("Settings saved", str(response.data))
self.assertIn("entered value is not a valid email address", str(response.data))

def test_github_redirect(self):
"""
Test editing account where github token is not null
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin, self.user.github_token)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/account/manage', data=dict(
current_password=self.user.password,
name="T1duS",
email=self.user.email
))
user = User.query.filter(User.name == "T1duS").first()
self.assertNotEqual(user, None)
self.assertIn("Settings saved", str(response.data))
123 changes: 120 additions & 3 deletions tests/test_ci/TestControllers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from mock import mock
from tests.base import BaseTestCase
from tests.base import BaseTestCase, MockRequests
from mod_test.models import Test, TestPlatform, TestType
from mod_regression.models import RegressionTest
from mod_customized.models import CustomizedTest
from mod_ci.models import BlockedUsers
from mod_auth.models import Role
from importlib import reload
from flask import g


class TestControllers(BaseTestCase):
@mock.patch('github.GitHub')
def test_comments_successfully_in_passed_pr_test(self, git_mock):
Expand Down Expand Up @@ -202,4 +202,121 @@ def test_inform_mailing_list(self, mock_email):
' Lorem Ipsum sit dolor amet...\n ',
'subject': 'GitHub Issue #matejmecka', 'to': 'ccextractor-dev@googlegroups.com'
}
)
)

@mock.patch('requests.get', side_effect=MockRequests)
def test_add_blocked_users(self, mock_request):
"""
Check adding a user to block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/blocked_users', data=dict(user_id=1, comment="Bad user", add=True))
self.assertNotEqual(BlockedUsers.query.filter(BlockedUsers.user_id == 1).first(), None)
with c.session_transaction() as session:
flash_message = dict(session['_flashes']).get('message')
self.assertEqual(flash_message, "User blocked successfully.")

@mock.patch('requests.get', side_effect=MockRequests)
def test_add_blocked_users_wrong_id(self, mock_request):
"""
Check adding invalid user id to block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/blocked_users', data=dict(user_id=0, comment="Bad user", add=True))
self.assertEqual(BlockedUsers.query.filter(BlockedUsers.user_id == 0).first(), None)
self.assertIn("GitHub User ID not filled in", str(response.data))

@mock.patch('requests.get', side_effect=MockRequests)
def test_add_blocked_users_empty_id(self, mock_request):
"""
Check adding blank user id to block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/blocked_users', data=dict(comment="Bad user", add=True))
self.assertEqual(BlockedUsers.query.filter(BlockedUsers.user_id == None).first(), None)
self.assertIn("GitHub User ID not filled in", str(response.data))

@mock.patch('requests.get', side_effect=MockRequests)
def test_add_blocked_users_already_exists(self, mock_request):
"""
Check adding existing blocked user again.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
blocked_user = BlockedUsers(1, "Bad user")
g.db.add(blocked_user)
g.db.commit()
response = c.post(
'/blocked_users', data=dict(user_id=1, comment="Bad user", add=True))
with c.session_transaction() as session:
flash_message = dict(session['_flashes']).get('message')
self.assertEqual(flash_message, "User already blocked.")

@mock.patch('requests.get', side_effect=MockRequests)
def test_remove_blocked_users(self, mock_request):
"""
Check removing user from block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
blocked_user = BlockedUsers(1, "Bad user")
g.db.add(blocked_user)
g.db.commit()
self.assertNotEqual(BlockedUsers.query.filter(BlockedUsers.comment == "Bad user").first(), None)
response = c.post(
'/blocked_users', data=dict(user_id=1, remove=True))
self.assertEqual(BlockedUsers.query.filter(BlockedUsers.user_id == 1).first(), None)
with c.session_transaction() as session:
flash_message = dict(session['_flashes']).get('message')
self.assertEqual(flash_message, "User removed successfully.")

@mock.patch('requests.get', side_effect=MockRequests)
def test_remove_blocked_users_wrong_id(self, mock_request):
"""
Check removing non existing id from block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/blocked_users', data=dict(user_id=7355608, remove=True))
with c.session_transaction() as session:
flash_message = dict(session['_flashes']).get('message')
self.assertEqual(flash_message, "No such user in Blacklist")

@mock.patch('requests.get', side_effect=MockRequests)
def test_remove_blocked_users_empty_id(self, mock_request):
"""
Check removing blank user id from block list.
"""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
response = c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.post(
'/blocked_users', data=dict(remove=True))
self.assertIn("GitHub User ID not filled in", str(response.data))

0 comments on commit a908ec0

Please sign in to comment.