Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests/common.py #1474

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion securedrop/.coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[run]
branch = True
source = .
omit = tests/*
omit = tests/*
120 changes: 0 additions & 120 deletions securedrop/tests/common.py

This file was deleted.

44 changes: 22 additions & 22 deletions securedrop/tests/functional/functional_test.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import unittest
from selenium import webdriver
from selenium.webdriver.firefox import firefox_binary
from selenium.common.exceptions import WebDriverException
# -*- coding: utf-8 -*-

from datetime import datetime
import mock
from multiprocessing import Process
import socket
import shutil
import os
import gnupg
import urllib2
import shutil
import signal
import socket
import sys
import time
import traceback
import unittest
import urllib2

import gnupg
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.firefox import firefox_binary

# Set environment variable so config.py uses a test environment
os.environ['SECUREDROP_ENV'] = 'test'
import config

import source
import db
import journalist
from tests import common
import urllib2

import signal
import traceback
from datetime import datetime
import time
import mock
import source
import tests.utils as utils


class FunctionalTest():
Expand Down Expand Up @@ -54,9 +54,9 @@ def setUp(self):

signal.signal(signal.SIGUSR1, lambda _, s: traceback.print_stack(s))

common.create_directories()
self.gpg = common.init_gpg()
common.init_db()
utils.env.create_directories()
self.gpg = utils.env.init_gpg()
db.init_db()

source_port = self._unused_port()
journalist_port = self._unused_port()
Expand Down Expand Up @@ -94,7 +94,7 @@ def start_journalist_server():
self.secret_message = 'blah blah blah'

def tearDown(self):
common.clean_root()
utils.env.teardown()
self.driver.quit()
self.source_process.terminate()
self.journalist_process.terminate()
Expand Down
64 changes: 25 additions & 39 deletions securedrop/tests/test_journalist.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import unittest
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from mock import patch, ANY, MagicMock
import unittest

from db import db_session, InvalidPasswordLength, Journalist
import journalist
import common
from db import Journalist, InvalidPasswordLength, db_session
import utils

class TestJournalist(unittest.TestCase):
class TestJournalistApp(unittest.TestCase):

def setUp(self):
journalist.logged_in = MagicMock()
Expand All @@ -18,10 +21,15 @@ def setUp(self):
journalist.get_docs = MagicMock()
journalist.get_or_else = MagicMock()

def _set_up_request(self, cols_selected, action):
journalist.request.form.__contains__.return_value = True
journalist.request.form.getlist = MagicMock(return_value=cols_selected)
journalist.request.form.__getitem__.return_value = action

@patch("journalist.col_delete")
def test_col_process_delegates_to_col_delete(self, col_delete):
cols_selected = ['source_id']
self.set_up_request(cols_selected, 'delete')
self._set_up_request(cols_selected, 'delete')

journalist.col_process()

Expand All @@ -30,7 +38,7 @@ def test_col_process_delegates_to_col_delete(self, col_delete):
@patch("journalist.col_star")
def test_col_process_delegates_to_col_star(self, col_star):
cols_selected = ['source_id']
self.set_up_request(cols_selected, 'star')
self._set_up_request(cols_selected, 'star')

journalist.col_process()

Expand All @@ -39,7 +47,7 @@ def test_col_process_delegates_to_col_star(self, col_star):
@patch("journalist.col_un_star")
def test_col_process_delegates_to_col_un_star(self, col_un_star):
cols_selected = ['source_id']
self.set_up_request(cols_selected, 'un-star')
self._set_up_request(cols_selected, 'un-star')

journalist.col_process()

Expand All @@ -48,7 +56,7 @@ def test_col_process_delegates_to_col_un_star(self, col_un_star):
@patch("journalist.abort")
def test_col_process_returns_404_with_bad_action(self, abort):
cols_selected = ['source_id']
self.set_up_request(cols_selected, 'something-random')
self._set_up_request(cols_selected, 'something-random')

journalist.col_process()

Expand All @@ -67,10 +75,6 @@ def test_col_un_star_call_db(self, db_session):

db_session.commit.assert_called_with()

def set_up_request(self, cols_selected, action):
journalist.request.form.__contains__.return_value = True
journalist.request.form.getlist = MagicMock(return_value=cols_selected)
journalist.request.form.__getitem__.return_value = action

@classmethod
def tearDownClass(cls):
Expand All @@ -82,29 +86,15 @@ def tearDownClass(cls):
class TestJournalistLogin(unittest.TestCase):

def setUp(self):
common.shared_setup()
utils.env.setup()

# Patch the two-factor verification so it always succeeds
patcher = patch('db.Journalist.verify_token')
self.addCleanup(patcher.stop)
self.mock_journalist_verify_token = patcher.start()
self.mock_journalist_verify_token.return_value = True

self.username = "test user"
self.password = "test password"
self.user = Journalist(
username=self.username,
password=self.password)
db_session.add(self.user)
db_session.commit()

# Use a patched login function to avoid dealing with two-factor tokens
# (which are being ignored here anyway)
self.login = lambda username, password: \
Journalist.login(username, password, "")
utils.db_helper.mock_verify_token(self)

self.user, self.user_pw = utils.db_helper.init_journalist()

def tearDown(self):
common.shared_teardown()
utils.env.teardown()
# TODO: figure out why this is necessary here, but unnecessary in all
# of the tests in `tests/test_unit_*.py`. Without this, the session
# continues to return values even if the underlying database is deleted
Expand All @@ -113,19 +103,16 @@ def tearDown(self):

@patch('db.Journalist._scrypt_hash')
@patch('db.Journalist.valid_password', return_value=True)
def test_login_with_valid_length_password_calls_scrypt(
self, mock_scrypt_hash, mock_valid_password):
self.login(self.username, self.password)
def test_valid_login_calls_scrypt(self, mock_scrypt_hash, mock_valid_password):
Journalist.login(self.user.username, self.user_pw, 'mocked')
self.assertTrue(mock_scrypt_hash.called,
"Failed to call _scrypt_hash for password w/ valid length")

@patch('db.Journalist._scrypt_hash')
def test_login_with_invalid_length_password_doesnt_call_scrypt(
self, mock_scrypt_hash):
print "test_login_with_invalid_length_password_calls_scrypt"
def test_login_with_invalid_password_doesnt_call_scrypt(self, mock_scrypt_hash):
invalid_pw = 'a'*(Journalist.MAX_PASSWORD_LEN + 1)
with self.assertRaises(InvalidPasswordLength):
self.login(self.username, invalid_pw)
Journalist.login(self.user.username, invalid_pw, 'mocked')
self.assertFalse(mock_scrypt_hash.called,
"Called _scrypt_hash for password w/ invalid length")

Expand All @@ -134,4 +121,3 @@ def tearDownClass(cls):
# Reset the module variables that were changed to mocks so we don't
# break other tests
reload(journalist)

9 changes: 6 additions & 3 deletions securedrop/tests/test_unit_crypto_util.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest

# Set environment variable so config.py uses a test environment
os.environ['SECUREDROP_ENV'] = 'test'

import config
import common
import crypto_util
import utils


class TestCryptoUtil(unittest.TestCase):

"""The set of tests for crypto_util.py."""

def setUp(self):
common.shared_setup()
utils.env.setup()

def tearDown(self):
common.shared_teardown()
utils.env.teardown()

def test_clean(self):
with self.assertRaises(crypto_util.CryptoException):
Expand Down
Loading