diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/gntest.rb | 6 | ||||
-rw-r--r-- | test/lib/link_checker.rb | 50 | ||||
-rw-r--r-- | test/lib/main_web_functionality.rb | 62 | ||||
-rw-r--r-- | test/lib/mapping.rb | 58 | ||||
-rw-r--r-- | test/lib/navigation.rb | 43 | ||||
-rw-r--r-- | test/requests/link_checker.py | 81 | ||||
-rw-r--r-- | test/requests/main_web_functionality.py | 40 | ||||
-rw-r--r-- | test/requests/mapping_tests.py | 52 | ||||
-rw-r--r-- | test/requests/navigation_tests.py | 15 | ||||
-rw-r--r-- | test/requests/parametrized_test.py | 32 | ||||
-rwxr-xr-x | test/requests/test-website.py | 119 | ||||
-rw-r--r-- | test/requests/test_forgot_password.py | 52 | ||||
-rw-r--r-- | test/requests/test_login_github.py | 47 | ||||
-rw-r--r-- | test/requests/test_login_local.py | 60 | ||||
-rw-r--r-- | test/requests/test_login_orcid.py | 47 | ||||
-rw-r--r-- | test/requests/test_registration.py | 41 | ||||
-rw-r--r-- | test/unittest/test_registration.py | 27 |
17 files changed, 613 insertions, 219 deletions
diff --git a/test/lib/gntest.rb b/test/lib/gntest.rb deleted file mode 100644 index 5b21b3d5..00000000 --- a/test/lib/gntest.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'minitest/autorun' -require 'mechanize' -require 'json' - -# ---- Use some default parameters if not set -$host = "http://localhost:5003" if !$host diff --git a/test/lib/link_checker.rb b/test/lib/link_checker.rb deleted file mode 100644 index 36cb36c8..00000000 --- a/test/lib/link_checker.rb +++ /dev/null @@ -1,50 +0,0 @@ - -SKIP = "mailto:" - -class LinkChecker -end - -describe LinkChecker do - before do - @agent = Mechanize.new - @agent.agent.http.ca_file = '/etc/ssl/certs/ca-certificates.crt' - end - - it "Get to front page" do - page = @agent.get($host) - page.links.each do |link| - puts link.href - if link.href !~ /#{SKIP}/ # /static\/dbdoc\/Hippocampus/ and link.href !~ /mailto:|glossary.html|sample_r|grits.eecs.utk.edu|correlationAnnotation.html/ - # Fetch link, program will crash with exception if link is broken - linkpage = @agent.get(link.href) - puts "Link to #{link.href} is valid, response code #{linkpage.code}" - end - end - - end - - describe LinkChecker do - it "Get to trait page" do - page = @agent.get($host+'/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P') - # p page - - # Get to trait page for 1435395_s_at - # form2 = show_trait_page.forms_with("trait_page")[0] - # form2.fields[30].name.must_equal "variance:C57BL/6J" - # form2.fields[30].value.must_equal "15.287" - - # Test every link on the page to check if it's broken or not - page.links.each do |link| - puts link.href - if link.href !~ /#{SKIP}/ # /static\/dbdoc\/Hippocampus/ and link.href !~ /mailto:|glossary.html|sample_r|grits.eecs.utk.edu|correlationAnnotation.html/ - # Fetch link, program will crash with exception if link is broken - linkpage = @agent.get(link.href) - puts "Link to #{link.href} is valid, response code #{linkpage.code}" - end - end - - end - - end - -end diff --git a/test/lib/main_web_functionality.rb b/test/lib/main_web_functionality.rb deleted file mode 100644 index 9ae49995..00000000 --- a/test/lib/main_web_functionality.rb +++ /dev/null @@ -1,62 +0,0 @@ -# In these tests we navigate from the main page to search - -class MainWebFunctionality -end - -describe MainWebFunctionality do - before do - @agent = Mechanize.new - @agent.agent.http.ca_file = '/etc/ssl/certs/ca-certificates.crt' - end - - describe MainWebFunctionality do - - it "Get to trait page" do - page = @agent.get($host) - p page - form = page.forms[1] - form.buttons[0].value.must_equal "Search" # main menu is loaded - - # http://localhost:5003/search?species=mouse&group=BXD&type=Hippocampus+mRNA&dataset=HC_M2_0606_P&search_terms_or=&search_terms_and=MEAN%3D%2815+16%29+LRS%3D%2823+46%29&FormID=searchResult - form.fields[0].value.must_equal "searchResult" - form.fields[2].value = "MEAN=(15 16) LRS=(23 46)" - form.fields[3].value = "mouse" - form.fields[4].value = "BXD" - form.fields[5].value = "Hippocampus mRNA" - form.fields[6].value = "HC_M2_0606_P" - search_page = @agent.submit(form, form.buttons.first) - p "==================" - p search_page - probe_link = search_page.links.find { |l| l.text =~ /1435395_s_at/ } - probe_link.uri.to_s.must_equal "/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P" - show_trait_page = probe_link.click - # p show_trait_page - - # Get to trait page for 1435395_s_at - - form2 = show_trait_page.forms_with("trait_page")[0] - # [name: corr_dataset value: HC_M2_0606_P] - form2.fields.select { |fld| fld.name == 'corr_dataset' }.first.value.must_equal 'HC_M2_0606_P' - if $options[:database] == :small - form2.fields[30].name.must_equal "value:DBA/2J" - else - form2.fields[30].name.must_equal "variance:C57BL/6J" - end - # form2.fields[30].value.must_equal "15.287" - - # Test every link on the page to check if it's broken or not - break if not $options[:link_checker] - show_trait_page.links.each do |link| - puts link.href - if link.href !~ /static\/dbdoc\/Hippocampus/ and link.href !~ /glossary.html|sample_r|grits.eecs.utk.edu|correlationAnnotation.html/ - # Fetch link, program will crash with exception if link is broken - linkpage = @agent.get(link.href) - puts "Link to #{link.href} is valid, response code #{linkpage.code}" - end - end - - end - - end - -end diff --git a/test/lib/mapping.rb b/test/lib/mapping.rb deleted file mode 100644 index 20e5bd40..00000000 --- a/test/lib/mapping.rb +++ /dev/null @@ -1,58 +0,0 @@ -# In these tests we navigate from the main page to a specific trait then hit the different mapping tool buttons (In this case pylMM and r/qtl) followed by computing the results (marker regressions). - -require 'gntest' - -class MappingTest -end - -describe MappingTest do - before do - @agent = Mechanize.new - @agent.agent.http.ca_file = '/etc/ssl/certs/ca-certificates.crt' - end - - describe MappingTest do - it "pylmm mapping tool selection" do - url = $host+'/marker_regression' - - json = JSON::load(File.read('test/data/input/mapping/1435395_s_at_HC_M2_0606_P.json')) - json["method"] = "pylmm" - # p json - page = @agent.post(URI.encode(url), json) - # Unpacking the page is slow - somehow - but the run is enough as a test - # form = page.forms_with("marker_regression")[0] - # form.fields.select { |fld| fld.name == 'dataset' }.first.value.must_equal 'HC_M2_0606_P' - # form.fields.select { |fld| fld.name == 'value:BXD1' }.first.value.must_equal '6.749' - end - end - - describe MappingTest do - it "R/qtl mapping tool selection" do - url = $host+'/marker_regression' # ?trait_id=1435395_s_at&dataset=HC_M2_0606_P' - - json = JSON::load(File.read('test/data/input/mapping/1435395_s_at_HC_M2_0606_P.json')) - # p json - page = @agent.post(URI.encode(url), - json, - ({'Content-Type' => 'application/x-www-form-urlencoded'})) - form = page.forms_with("marker_regression")[0] - p form - form.fields.select { |fld| fld.name == 'dataset' }.first.value.must_equal 'HC_M2_0606_P' - form.fields.select { |fld| fld.name == 'value:BXD1' }.first.value.must_equal "15.034" - end - end - - describe MappingTest do - it "CIM mapping tool selection (using reaper)" do - url = $host+'/marker_regression' - - json = JSON::load(File.read('test/data/input/mapping/1435395_s_at_HC_M2_0606_P.json')) - json["method"] = "reaper" - page = @agent.post(URI.encode(url), json) - form = page.forms_with("marker_regression")[0] - form.fields.select { |fld| fld.name == 'dataset' }.first.value.must_equal 'HC_M2_0606_P' - form.fields.select { |fld| fld.name == 'value:BXD1' }.first.value.must_equal "15.034" - end - end - -end diff --git a/test/lib/navigation.rb b/test/lib/navigation.rb deleted file mode 100644 index ee9f41d2..00000000 --- a/test/lib/navigation.rb +++ /dev/null @@ -1,43 +0,0 @@ -# In these tests we navigate from the main page to a specific trait then hit the different mapping tool buttons (In this case pylMM and r/qtl) followed by computing the results (marker regressions). - - -class MappingTest -end - -describe MappingTest do - before do - @agent = Mechanize.new - @agent.agent.http.ca_file = '/etc/ssl/certs/ca-certificates.crt' - end - - describe MappingTest do - it "pyLMM mapping tool selection" do - break if $options[:skip_broken] - page = @agent.get($host+'/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P') -#Navigates to http://localhost:5003/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P and clicks respective buttons. - link = page.link_with(text: 'pyLMM') - page = link.click - puts page.uri - link = page.link_with(text: 'Compute') - page = link.click - puts page.uri - probe_link.uri.to_s.must_equal "/marker_regression" - end - end - -end - -describe MappingTest do - it "R/qtl mapping tool selection" do - break if $options[:skip_broken] - page = @agent.get($host+'/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P') - link = page.link_with(text: 'R/qtl') - page = link.click - puts page.uri - form.field_with(:name => 'Methods').options[2].select - link = page.link_with(text: 'Compute') - page = link.click - puts page.uri - probe_link.uri.to_s.must_equal "/marker_regression" - end -end diff --git a/test/requests/link_checker.py b/test/requests/link_checker.py new file mode 100644 index 00000000..715f330c --- /dev/null +++ b/test/requests/link_checker.py @@ -0,0 +1,81 @@ +from __future__ import print_function +import re +import requests +from lxml.html import parse +from requests.exceptions import ConnectionError + +DO_FAIL=False # fail on error + +def is_root_link(link): + pattern = re.compile("^/$") + return pattern.match(link) + +def is_mailto_link(link): + pattern = re.compile("^mailto:.*") + return pattern.match(link) + +def is_internal_link(link): + pattern = re.compile("^/.*") + return pattern.match(link) + +def is_in_page_link(link): + pattern = re.compile("^#.*") + return pattern.match(link) + +def get_links(doc): + return filter( + lambda x: not ( + is_root_link(x) + or is_mailto_link(x)) + , map(lambda y: y.get("href") + , doc.cssselect("a"))) + +def verify_link(link): + if link[0] == "#": + # local link on page + return + print("verifying "+link) + try: + result = requests.get(link, timeout=20, verify=False) + if result.status_code == 200: + print(link+" ==> OK") + elif result.status_code == 307: + print(link+" ==> REDIRECT") + else: + print("ERROR: link `"+link+"` failed with status " + , result.status_code) + + if DO_FAIL: + raise Exception("Failed verify") + except ConnectionError as ex: + print("ERROR: ", link, ex) + if DO_FAIL: + raise ex + +def check_page(host, start_url): + print("") + print("Checking links host "+host+" in page `"+start_url+"`") + doc = parse(start_url).getroot() + links = get_links(doc) + in_page_links = filter(is_in_page_link, links) + internal_links = filter(is_internal_link, links) + external_links = filter(lambda x: not (is_internal_link(x) or is_in_page_link(x)), links) + + for link in internal_links: + verify_link(host+link) + + for link in external_links: + verify_link(link) + +def check_links(args_obj, parser): + print("") + print("Checking links") + host = args_obj.host + + # Check the home page + check_page(host, host) + + # Check traits page + check_page( + host, + host+"/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P") diff --git a/test/requests/main_web_functionality.py b/test/requests/main_web_functionality.py new file mode 100644 index 00000000..7b89b833 --- /dev/null +++ b/test/requests/main_web_functionality.py @@ -0,0 +1,40 @@ +from __future__ import print_function +import re +import requests +from lxml.html import parse +from link_checker import check_page +from requests.exceptions import ConnectionError + +def check_home(url): + doc = parse(url).getroot() + search_button = doc.cssselect("#btsearch") + assert(search_button[0].value == "Search") + print("OK") + +def check_search_page(host): + data = dict( + species="mouse" + , group="BXD" + , type="Hippocampus mRNA" + , dataset="HC_M2_0606_P" + , search_terms_or="" + , search_terms_and="MEAN=(15 16) LRS=(23 46)") + result = requests.get(host+"/search", params=data) + found = result.text.find("/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P") + assert(found >= 0) + print("OK") + check_traits_page(host, "/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P") + +def check_traits_page(host, traits_url): + doc = parse(host+traits_url).getroot() + traits_form = doc.forms[1] + assert(traits_form.fields["corr_dataset"] == "HC_M2_0606_P") + print("OK") + check_page(host, host+traits_url) + +def check_main_web_functionality(args_obj, parser): + print("") + print("Checking main web functionality...") + host = args_obj.host + check_home(host) + check_search_page(host) diff --git a/test/requests/mapping_tests.py b/test/requests/mapping_tests.py new file mode 100644 index 00000000..8eb19de7 --- /dev/null +++ b/test/requests/mapping_tests.py @@ -0,0 +1,52 @@ +from __future__ import print_function +import re +import copy +import json +import requests +from lxml.html import fromstring + +def load_data_from_file(): + filename = "../test/data/input/mapping/1435395_s_at_HC_M2_0606_P.json" + file_handle = open(filename, "r") + file_data = json.loads(file_handle.read().encode("utf-8")) + return file_data + +def check_pylmm_tool_selection(host, data): + print("") + print("pylmm mapping tool selection") + data["method"] = "pylmm" + page = requests.post(host+"/marker_regression", data=data) + doc = fromstring(page.text) + form = doc.forms[1] + assert form.fields["dataset"] == "HC_M2_0606_P" + assert form.fields["value:BXD1"] == "15.034" # Check value in the file + +def check_R_qtl_tool_selection(host, data): + print("") + print("R/qtl mapping tool selection") + headers = {"Content-Type": "application/x-www-form-urlencoded"} + page = requests.post(host+"/marker_regression", data=data, headers=headers) + doc = fromstring(page.text) + form = doc.forms[1] + assert form.fields["dataset"] == "HC_M2_0606_P" + assert form.fields["value:BXD1"] == "15.034" + +def check_CIM_tool_selection(host, data): + print("") + print("CIM mapping tool selection (using reaper)") + data["method"] = "reaper" + page = requests.post(host+"/marker_regression", data=data) + doc = fromstring(page.text) + form = doc.forms[1] + assert form.fields["dataset"] == "HC_M2_0606_P" + assert form.fields["value:BXD1"] == "15.034" + +def check_mapping(args_obj, parser): + print("") + print("Checking mapping") + + host = args_obj.host + data = load_data_from_file() + check_pylmm_tool_selection(host, copy.deepcopy(data)) + check_R_qtl_tool_selection(host, copy.deepcopy(data)) ## Why does this fail? + check_CIM_tool_selection(host, copy.deepcopy(data)) diff --git a/test/requests/navigation_tests.py b/test/requests/navigation_tests.py new file mode 100644 index 00000000..eda27324 --- /dev/null +++ b/test/requests/navigation_tests.py @@ -0,0 +1,15 @@ +from __future__ import print_function +import re +import requests +from lxml.html import parse + +def check_navigation(args_obj, parser): + print("") + print("Checking navigation.") + + host = args_obj.host + url = host + "/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P" + print("URL: ", url) + page = requests.get(url) + # Page is built by the javascript, hence using requests fails for this. + # Investigate use of selenium maybe? diff --git a/test/requests/parametrized_test.py b/test/requests/parametrized_test.py new file mode 100644 index 00000000..50003850 --- /dev/null +++ b/test/requests/parametrized_test.py @@ -0,0 +1,32 @@ +import logging +import unittest +from wqflask import app +from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column +from elasticsearch import Elasticsearch, TransportError + +class ParametrizedTest(unittest.TestCase): + + def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"): + super(ParametrizedTest, self).__init__(methodName=methodName) + self.gn2_url = gn2_url + self.es_url = es_url + + def setUp(self): + self.es = get_elasticsearch_connection() + self.es_cleanup = [] + + es_logger = logging.getLogger("elasticsearch") + es_logger.setLevel(app.config.get("LOG_LEVEL")) + es_logger.addHandler( + logging.FileHandler("/tmp/es_TestRegistrationInfo.log")) + es_trace_logger = logging.getLogger("elasticsearch.trace") + es_trace_logger.addHandler( + logging.FileHandler("/tmp/es_TestRegistrationTrace.log")) + + def tearDown(self): + from time import sleep + self.es.delete_by_query( + index="users" + , doc_type="local" + , body={"query":{"match":{"email_address":"test@user.com"}}}) + sleep(1) diff --git a/test/requests/test-website.py b/test/requests/test-website.py new file mode 100755 index 00000000..b2e09bc4 --- /dev/null +++ b/test/requests/test-website.py @@ -0,0 +1,119 @@ +# Run with something like +# +# env GN2_PROFILE=/home/wrk/opt/gn-latest ./bin/genenetwork2 ./etc/default_settings.py -c ../test/requests/test-website.py http://localhost:5003 +# +# Mostly to pick up the Guix GN2_PROFILE and python modules +from __future__ import print_function +import argparse +from link_checker import check_links +from mapping_tests import check_mapping +from navigation_tests import check_navigation +from main_web_functionality import check_main_web_functionality +import link_checker +import sys + +# Imports for integration tests +from wqflask import app +from test_login_local import TestLoginLocal +from test_login_orcid import TestLoginOrcid +from test_login_github import TestLoginGithub +from test_registration import TestRegistration +from test_forgot_password import TestForgotPassword +from unittest import TestSuite, TextTestRunner, TestLoader + +print("Mechanical Rob firing up...") + +def run_all(args_obj, parser): + print("") + print("Running all tests.") + print(args_obj) + link_checker.DO_FAIL = args_obj.fail + check_main_web_functionality(args_obj, parser) + check_links(args_obj, parser) + check_mapping(args_obj, parser) + # TODO: Add other functions as they are created. + +def print_help(args_obj, parser): + print(parser.format_help()) + +def dummy(args_obj, parser): + print("Not implemented yet.") + +def integration_tests(args_obj, parser): + gn2_url = args_obj.host + es_url = app.config.get("ELASTICSEARCH_HOST")+":"+str(app.config.get("ELASTICSEARCH_PORT")) + run_integration_tests(gn2_url, es_url) + +def initTest(klass, gn2_url, es_url): + loader = TestLoader() + methodNames = loader.getTestCaseNames(klass) + return [klass(mname, gn2_url, es_url) for mname in methodNames] + +def integration_suite(gn2_url, es_url): + test_cases = [ + TestRegistration + , TestLoginLocal + , TestLoginGithub + , TestLoginOrcid + , TestForgotPassword + ] + the_suite = TestSuite() + for case in test_cases: + the_suite.addTests(initTest(case, gn2_url, es_url)) + return the_suite + +def run_integration_tests(gn2_url, es_url): + runner = TextTestRunner() + runner.run(integration_suite(gn2_url, es_url)) + + +desc = """ +This is Mechanical-Rob - an automated web server tester for + Genenetwork.org +""" +parser = argparse.ArgumentParser(description=desc) + +parser.add_argument("--fail", help="Fail and stop on any error", action="store_true") + +parser.add_argument("-d", "--database", metavar="DB", type=str + , default="db_webqtl_s" + , help="Use database (default db_webqtl_s)") + +parser.add_argument("host", metavar="HOST", type=str + , default="http://localhost:5003" + , help="The url to the web server") + +parser.add_argument("-a", "--all", dest="accumulate", action="store_const" + , const=run_all, default=print_help + , help="Runs all tests.") + +parser.add_argument("-l", "--link-checker", dest="accumulate" + , action='store_const', const=check_links, default=print_help + , help="Checks for dead links.") + +parser.add_argument("-f", "--main-functionality", dest="accumulate" + , action='store_const', const=check_main_web_functionality + , default=print_help + , help="Checks for main web functionality.") + +parser.add_argument("-m", "--mapping", dest="accumulate" + , action="store_const", const=check_mapping, default=print_help + , help="Checks for mapping.") + +parser.add_argument("-i", "--integration-tests", dest="accumulate" + , action="store_const", const=integration_tests, default=print_help + , help="Runs integration tests.") + +# Navigation tests deactivated since system relies on Javascript +# parser.add_argument("-n", "--navigation", dest="accumulate" +# , action="store_const", const=check_navigation, default=print_help +# , help="Checks for navigation.") + +# parser.add_argument("-s", "--skip-broken", dest="accumulate" +# , action="store_const", const=dummy, default=print_help +# , help="Skip tests that are known to be broken.") + +args = parser.parse_args() +# print("The arguments object: ", args) + +args.accumulate(args, parser) diff --git a/test/requests/test_forgot_password.py b/test/requests/test_forgot_password.py new file mode 100644 index 00000000..2bf34c5c --- /dev/null +++ b/test/requests/test_forgot_password.py @@ -0,0 +1,52 @@ +import requests +from wqflask import user_manager +from utility.elasticsearch_tools import get_user_by_unique_column +from parameterized import parameterized +from parametrized_test import ParametrizedTest + +passwork_reset_link = '' +forgot_password_page = None + +class TestForgotPassword(ParametrizedTest): + + def setUp(self): + super(TestForgotPassword, self).setUp() + self.forgot_password_url = self.gn2_url+"/n/forgot_password_submit" + def send_email(to_addr, msg, fromaddr="no-reply@genenetwork.org"): + print("CALLING: send_email_mock()") + email_data = { + "to_addr": to_addr + , "msg": msg + , "fromaddr": from_addr} + + data = { + "es_connection": self.es, + "email_address": "test@user.com", + "full_name": "Test User", + "organization": "Test Organisation", + "password": "test_password", + "password_confirm": "test_password" + } + user_manager.basic_info = lambda : { "basic_info": "basic" } + user_manager.RegisterUser(data) + + def testWithoutEmail(self): + data = {"email_address": ""} + error_notification = '<div class="alert alert-danger">You MUST provide an email</div>' + result = requests.post(self.forgot_password_url, data=data) + self.assertEqual(result.url, self.gn2_url+"/n/forgot_password") + self.assertTrue( + result.content.find(error_notification) >= 0 + , "Error message should be displayed but was not") + + def testWithNonExistingEmail(self): + # Monkey patching doesn't work, so simply test that getting by email + # returns the correct data + user = get_user_by_unique_column(self.es, "email_address", "non-existent@domain.com") + self.assertTrue(user is None, "Should not find non-existent user") + + def testWithExistingEmail(self): + # Monkey patching doesn't work, so simply test that getting by email + # returns the correct data + user = get_user_by_unique_column(self.es, "email_address", "test@user.com") + self.assertTrue(user is not None, "Should find user") diff --git a/test/requests/test_login_github.py b/test/requests/test_login_github.py new file mode 100644 index 00000000..1bf4f695 --- /dev/null +++ b/test/requests/test_login_github.py @@ -0,0 +1,47 @@ +import uuid +import requests +from time import sleep +from wqflask import app +from parameterized import parameterized +from parametrized_test import ParametrizedTest + +login_link_text = '<a id="login_in" href="/n/login">Sign in</a>' +logout_link_text = '<a id="login_out" title="Signed in as ." href="/n/logout">Sign out</a>' +uid = str(uuid.uuid4()) + +class TestLoginGithub(ParametrizedTest): + + def setUp(self): + super(TestLoginGithub, self).setUp() + data = { + "user_id": uid + , "name": "A. T. Est User" + , "github_id": 693024 + , "user_url": "https://fake-github.com/atestuser" + , "login_type": "github" + , "organization": "" + , "active": 1 + , "confirmed": 1 + } + self.es.create(index="users", doc_type="local", body=data, id=uid) + sleep(1) + + def tearDown(self): + super(TestLoginGithub, self).tearDown() + self.es.delete(index="users", doc_type="local", id=uid) + + def testLoginUrl(self): + login_button_text = '<a href="https://github.com/login/oauth/authorize?client_id=' + app.config.get("GITHUB_CLIENT_ID") + '&client_secret=' + app.config.get("GITHUB_CLIENT_SECRET") + '" title="Login with GitHub" class="btn btn-info btn-group">Login with Github</a>' + result = requests.get(self.gn2_url+"/n/login") + index = result.content.find(login_button_text) + self.assertTrue(index >= 0, "Should have found `Login with Github` button") + + @parameterized.expand([ + ("1234", login_link_text, "Login should have failed with non-existing user") + , (uid, logout_link_text, "Login should have been successful with existing user") + ]) + def testLogin(self, test_uid, expected, message): + url = self.gn2_url+"/n/login?type=github&uid="+test_uid + result = requests.get(url) + index = result.content.find(expected) + self.assertTrue(index >= 0, message) diff --git a/test/requests/test_login_local.py b/test/requests/test_login_local.py new file mode 100644 index 00000000..808649ca --- /dev/null +++ b/test/requests/test_login_local.py @@ -0,0 +1,60 @@ +import requests +from wqflask import user_manager +from parameterized import parameterized +from parametrized_test import ParametrizedTest + +login_link_text = '<a id="login_in" href="/n/login">Sign in</a>' +logout_link_text = '<a id="login_out" title="Signed in as ." href="/n/logout">Sign out</a>' + +class TestLoginLocal(ParametrizedTest): + + def setUp(self): + super(TestLoginLocal, self).setUp() + self.login_url = self.gn2_url +"/n/login" + data = { + "es_connection": self.es, + "email_address": "test@user.com", + "full_name": "Test User", + "organization": "Test Organisation", + "password": "test_password", + "password_confirm": "test_password" + } + user_manager.basic_info = lambda : { "basic_info": "basic" } + user_manager.RegisterUser(data) + + + @parameterized.expand([ + ( + { + "email_address": "non@existent.email", + "password": "doesitmatter?" + }, login_link_text, "Login should have failed with the wrong user details."), + ( + { + "email_address": "test@user.com", + "password": "test_password" + }, logout_link_text, "Login should have been successful with correct user details and neither import_collections nor remember_me set"), + ( + { + "email_address": "test@user.com", + "password": "test_password", + "import_collections": "y" + }, logout_link_text, "Login should have been successful with correct user details and only import_collections set"), + ( + { + "email_address": "test@user.com", + "password": "test_password", + "remember_me": "y" + }, logout_link_text, "Login should have been successful with correct user details and only remember_me set"), + ( + { + "email_address": "test@user.com", + "password": "test_password", + "remember_me": "y", + "import_collections": "y" + }, logout_link_text, "Login should have been successful with correct user details, and both remember_me, and import_collections set") + ]) + def testLogin(self, data, expected, message): + result = requests.post(self.login_url, data=data) + index = result.content.find(expected) + self.assertTrue(index >= 0, message) diff --git a/test/requests/test_login_orcid.py b/test/requests/test_login_orcid.py new file mode 100644 index 00000000..ea15642e --- /dev/null +++ b/test/requests/test_login_orcid.py @@ -0,0 +1,47 @@ +import uuid +import requests +from time import sleep +from wqflask import app +from parameterized import parameterized +from parametrized_test import ParametrizedTest + +login_link_text = '<a id="login_in" href="/n/login">Sign in</a>' +logout_link_text = '<a id="login_out" title="Signed in as ." href="/n/logout">Sign out</a>' +uid = str(uuid.uuid4()) + +class TestLoginOrcid(ParametrizedTest): + + def setUp(self): + super(TestLoginOrcid, self).setUp() + data = { + "user_id": uid + , "name": "A. T. Est User" + , "orcid": 345872 + , "user_url": "https://fake-orcid.org/atestuser" + , "login_type": "orcid" + , "organization": "" + , "active": 1 + , "confirmed": 1 + } + self.es.create(index="users", doc_type="local", body=data, id=uid) + sleep(1) + + def tearDown(self): + super(TestLoginOrcid, self).tearDown() + self.es.delete(index="users", doc_type="local", id=uid) + + def testLoginUrl(self): + login_button_text = 'a href="https://sandbox.orcid.org/oauth/authorize?response_type=code&scope=/authenticate&show_login=true&client_id=' + app.config.get("ORCID_CLIENT_ID") + '&client_secret=' + app.config.get("ORCID_CLIENT_SECRET") + '" title="Login with ORCID" class="btn btn-info btn-group">Login with ORCID</a>' + result = requests.get(self.gn2_url+"/n/login") + index = result.content.find(login_button_text) + self.assertTrue(index >= 0, "Should have found `Login with ORCID` button") + + @parameterized.expand([ + ("1234", login_link_text, "Login should have failed with non-existing user") + , (uid, logout_link_text, "Login should have been successful with existing user") + ]) + def testLogin(self, test_uid, expected, message): + url = self.gn2_url+"/n/login?type=orcid&uid="+test_uid + result = requests.get(url) + index = result.content.find(expected) + self.assertTrue(index >= 0, message) diff --git a/test/requests/test_registration.py b/test/requests/test_registration.py new file mode 100644 index 00000000..0047e8a6 --- /dev/null +++ b/test/requests/test_registration.py @@ -0,0 +1,41 @@ +import sys +import requests +from parametrized_test import ParametrizedTest + +class TestRegistration(ParametrizedTest): + + def tearDown(self): + for item in self.es_cleanup: + self.es.delete(index="users", doc_type="local", id=item["_id"]) + + def testRegistrationPage(self): + if self.es.ping(): + data = { + "email_address": "test@user.com", + "full_name": "Test User", + "organization": "Test Organisation", + "password": "test_password", + "password_confirm": "test_password" + } + requests.post(self.gn2_url+"/n/register", data) + response = self.es.search( + index="users" + , doc_type="local" + , body={ + "query": {"match": {"email_address": "test@user.com"}}}) + self.assertEqual(len(response["hits"]["hits"]), 1) + else: + self.skipTest("The elasticsearch server is down") + +def main(gn2, es): + import unittest + suite = unittest.TestSuite() + suite.addTest(TestRegistration(methodName="testRegistrationPage", gn2_url=gn2, es_url=es)) + runner = unittest.TextTestRunner() + runner.run(suite) + +if __name__ == "__main__": + if len(sys.argv) < 3: + raise Exception("Required arguments missing") + else: + main(sys.argv[1], sys.argv[2]) diff --git a/test/unittest/test_registration.py b/test/unittest/test_registration.py new file mode 100644 index 00000000..98d0cdff --- /dev/null +++ b/test/unittest/test_registration.py @@ -0,0 +1,27 @@ +# Run test with something like +# +# env GN2_PROFILE=~/opt/gn-latest GENENETWORK_FILES=$HOME/gn2_data ./bin/genenetwork2 ./etc/default_settings.py -c ../test/unittest/test_registration.py +# + +import unittest +import mock.es_double as es +from wqflask.user_manager import RegisterUser + +class TestRegisterUser(unittest.TestCase): + def setUp(self): + self.es = es.ESDouble() + + def testRegisterUserWithCorrectData(self): + data = { + "email_address": "user@example.com" + , "full_name": "A.N. Other" + , "organization": "Some Organisation" + , "password": "testing" + , "password_confirm": "testing" + , "es_connection": self.es + } + result = RegisterUser(data) + self.assertEqual(len(result.errors), 0, "Errors were not expected") + +if __name__ == "__main__": + unittest.main() |