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

Created Folder structure for tests for app #497

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1,574 changes: 0 additions & 1,574 deletions src/app/tests.py

This file was deleted.

Empty file added src/app/tests/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions src/app/tests/about_views_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-


from django.test import TestCase
from django.urls import reverse

class AboutViewsTestCase(TestCase):

def test_about(self):
"""GET Request for about"""
resp = self.client.get(reverse("about"),follow=True,secure=True)
self.assertEqual(resp.status_code,200)
self.assertEqual(resp.redirect_chain,[])
self.assertIn("app/about.html",(i.name for i in resp.templates))
self.assertEqual(resp.resolver_match.func.__name__,"about")
98 changes: 98 additions & 0 deletions src/app/tests/archieve_license_namespace_views_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-


from django.urls import reverse
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager

from app.models import LicenseNamespace
from app.generateXml import generateLicenseXml



class ArchiveLicenseNamespaceViewsTestCase(StaticLiveServerTestCase):

def setUp(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a common setup and teardown function in test_utils.py and use it wherever we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rtgdk I had gone through most of the test files in api and apps like mostly for them each setup and teardown functions are different only, so I feel this would not be of much help

options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
super(ArchiveLicenseNamespaceViewsTestCase, self).setUp()

def tearDown(self):
self.selenium.quit()
super(ArchiveLicenseNamespaceViewsTestCase, self).tearDown()

def test_archive_license_requests(self):
"""GET Request for archive license namespace requests list"""
resp = self.client.get(reverse("archive-license-namespace-xml"),follow=True,secure=True)
self.assertEqual(resp.status_code,200)
self.assertEqual(resp.redirect_chain,[])
self.assertIn("app/archive_namespace_requests.html",(i.name for i in resp.templates))
self.assertEqual(resp.resolver_match.func.__name__,"archiveNamespaceRequests")

def test_error_archive_license_namespace(self):
"""Check if error page is displayed when the license id does not exist for archive license namespace"""
license_id = 0
resp = self.client.get(reverse("archived-license-namespace-information", args=(license_id,)),follow=True,secure=True)
self.assertEqual(resp.status_code,404)
self.assertEqual(resp.redirect_chain,[])
self.assertIn("404.html",(i.name for i in resp.templates))
self.assertEqual(resp.resolver_match.func.__name__,"licenseNamespaceInformation")

def test_archive_license_namespace_feature(self):
"""Check if the license namespace is shifted to archive namespace when archive button is pressed"""
driver = self.selenium
driver.get(self.live_server_url+'/app/license_namespace_requests/')
table_contents = driver.find_element_by_css_selector('tbody').text
self.assertEqual(table_contents, "No data available in table")
xml = generateLicenseXml('', "0BSD", "BSD Zero Clause License-00",
'', "http://wwww.spdx.org", '', '', '')
license_obj = LicenseNamespace.objects.create(fullname="BSD Zero Clause License-00",
licenseAuthorName="John Doe",
shortIdentifier="0BSD",
archive="False",
url="http://wwww.spdx.org",
description="Description",
notes="Notes",
namespace="bsd-zero-clause-license-00",
userEmail="[email protected]",
publiclyShared="True",
license_list_url="http://wwww.spdx.org",
github_repo_url="http://wwww.spdx.org",
xml=xml)
driver.refresh()
license_name = driver.find_element_by_css_selector('td').text
self.assertEqual(license_name, "BSD Zero Clause License-00")
self.assertEqual(LicenseNamespace.objects.get(id=license_obj.id).archive, False)
driver.find_element_by_id('archive_button' + str(license_obj.id)).click()
driver.find_element_by_id('confirm_archive').click()
self.assertEqual(LicenseNamespace.objects.get(id=license_obj.id).archive, True)

def test_unarchive_license_namespace_feature(self):
"""Check if license namespace is shifted back to license namespace when unarchive button is pressed"""
driver = self.selenium
driver.get(self.live_server_url+'/app/archive_namespace_requests/')
table_contents = driver.find_element_by_css_selector('tbody').text
self.assertEqual(table_contents, "No data available in table")
archive_license_obj = LicenseNamespace.objects.create(fullname="BSD Zero Clause License-00",
licenseAuthorName="John Doe",
shortIdentifier="0BSD",
archive="True",
url="http://wwww.spdx.org",
description="Description",
notes="Notes",
namespace="bsd-zero-clause-license-00",
userEmail="[email protected]",
publiclyShared="True",
license_list_url="http://wwww.spdx.org",
github_repo_url="http://wwww.spdx.org")
driver.refresh()
license_name = driver.find_element_by_css_selector('td').text
self.assertEqual(license_name, "BSD Zero Clause License-00")
self.assertEqual(LicenseNamespace.objects.get(id=archive_license_obj.id).archive, True)
driver.find_element_by_id('unarchive_button' + str(archive_license_obj.id)).click()
driver.find_element_by_id('confirm_unarchive').click()
self.assertEqual(LicenseNamespace.objects.get(id=archive_license_obj.id).archive, False)
81 changes: 81 additions & 0 deletions src/app/tests/archive_license_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-


from unittest import skipIf
from src.secret import getAccessToken, getGithubUserId, getGithubUserName
from django.urls import reverse
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager

from app.models import LicenseRequest


class ArchiveLicenseRequestsViewsTestCase(StaticLiveServerTestCase):

def setUp(self):
options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
super(ArchiveLicenseRequestsViewsTestCase, self).setUp()

def tearDown(self):
self.selenium.quit()
super(ArchiveLicenseRequestsViewsTestCase, self).tearDown()

def test_archive_license_requests(self):
"""GET Request for archive license requests list"""
resp = self.client.get(reverse("archive-license-xml"),follow=True,secure=True)
self.assertEqual(resp.status_code,200)
self.assertEqual(resp.redirect_chain,[])
self.assertIn("app/archive_requests.html",(i.name for i in resp.templates))
self.assertEqual(resp.resolver_match.func.__name__,"archiveRequests")

def test_error_archive_license_requests(self):
"""Check if error page is displayed when the license id does not exist for archive license"""
license_id = 0
resp = self.client.get(reverse("archived-license-information", args=(license_id,)),follow=True,secure=True)
self.assertEqual(resp.status_code,404)
self.assertEqual(resp.redirect_chain,[])
self.assertIn("404.html",(i.name for i in resp.templates))
self.assertEqual(resp.resolver_match.func.__name__,"licenseInformation")

@skipIf(not getAccessToken() and not getGithubUserId() and not getGithubUserName(), "You need to set gihub parameters in the secret.py file for this test to be executed properly.")
def test_archive_license_requests_feature(self):
"""Check if the license is shifted to archive requests when archive button is pressed"""
driver = self.selenium
driver.get(self.live_server_url+'/app/license_requests/')
table_contents = driver.find_element_by_css_selector('tbody').text
self.assertEqual(table_contents, "No data available in table")
license_obj = LicenseRequest.objects.create(fullname="BSD Zero Clause License-00", shortIdentifier="0BSD")
driver.refresh()
license_name = driver.find_element_by_css_selector('td').text
self.assertEqual(license_name, "BSD Zero Clause License-00")
self.assertEqual(LicenseRequest.objects.get(id=license_obj.id).archive, False)
if driver.find_element_by_id('archive_button' + str(license_obj.id)):
driver.find_element_by_id('archive_button' + str(license_obj.id)).click()
driver.find_element_by_id('confirm_archive').click()
self.assertEqual(LicenseRequest.objects.get(id=license_obj.id).archive, True)
else:
pass

@skipIf(not getAccessToken() and not getGithubUserId() and not getGithubUserName(), "You need to set gihub parameters in the secret.py file for this test to be executed properly.")
def test_unarchive_license_requests_feature(self):
"""Check if license is shifted back to license requests when unarchive button is pressed"""
driver = self.selenium
driver.get(self.live_server_url+'/app/archive_requests/')
table_contents = driver.find_element_by_css_selector('tbody').text
self.assertEqual(table_contents, "No data available in table")
archive_license_obj = LicenseRequest.objects.create(fullname="BSD Zero Clause License-00", shortIdentifier="0BSD", archive="True")
driver.refresh()
license_name = driver.find_element_by_css_selector('td').text
self.assertEqual(license_name, "BSD Zero Clause License-00")
self.assertEqual(LicenseRequest.objects.get(id=archive_license_obj.id).archive, True)
if driver.find_element_by_id('unarchive_button' + str(archive_license_obj.id)):
driver.find_element_by_id('unarchive_button' + str(archive_license_obj.id)).click()
driver.find_element_by_id('confirm_unarchive').click()
self.assertEqual(LicenseRequest.objects.get(id=archive_license_obj.id).archive, False)
else:
pass
44 changes: 44 additions & 0 deletions src/app/tests/check_license_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-


from django.test import TestCase
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse


from django.contrib.auth.models import User
from django.conf import settings
import os


def getExamplePath(filename):
return os.path.join(settings.EXAMPLES_DIR, filename)

class CheckLicenseViewsTestCase(TestCase):

def setUp(self):
self.licensefile = open(getExamplePath("AFL-1.1.txt"))
self.licensetext = self.licensefile.read()

def test_check_license(self):
"""GET Request for check license"""
if not settings.ANONYMOUS_LOGIN_ENABLED :
resp = self.client.get(reverse("check-license"),follow=True,secure=True)
self.assertEqual(resp.status_code,200)
self.assertNotEqual(resp.redirect_chain,[])
self.assertIn(settings.LOGIN_URL, (i[0] for i in resp.redirect_chain))
self.client.force_login(User.objects.get_or_create(username='checklicensetestuser')[0])
resp2 = self.client.get(reverse("check-license"),follow=True,secure=True)
self.assertEqual(resp2.status_code,200)
self.assertEqual(resp2.redirect_chain,[])
self.assertIn("app/check_license.html",(i.name for i in resp2.templates))
self.assertEqual(resp2.resolver_match.func.__name__,"check_license")
self.client.logout()

# def test_post_check_license(self):
# self.client.force_login(User.objects.get_or_create(username='checklicensetestuser')[0])
# resp = self.client.post(reverse("check-license"),{'licensetext': self.licensetext},follow=True)
# self.assertEqual(resp.status_code,200)
# self.assertIn("success",resp.context)
# self.client.logout()
33 changes: 33 additions & 0 deletions src/app/tests/check_username_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-


from django.test import TestCase
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse


from django.contrib.auth.models import User
from django.conf import settings
import os


class CheckUserNameTestCase(TestCase):

def initialise(self):
self.username = "checktestuser"
self.password ="checktestpass"
self.credentials = {'username':self.username,'password':self.password }
User.objects.create_user(**self.credentials)

def test_check_username(self):
"""POST Request for checking username"""
resp = self.client.post(reverse("check-username"),{"username":"spdx"},follow=True,secure=True)
self.assertEqual(resp.status_code,200)

resp2 = self.client.post(reverse("check-username"),{"randomkey":"randomvalue"},follow=True,secure=True)
self.assertEqual(resp2.status_code,400)

self.initialise()
resp3 = self.client.post(reverse("check-username"),{"username":"checktestuser"},follow=True,secure=True)
self.assertEqual(resp3.status_code,404)
98 changes: 98 additions & 0 deletions src/app/tests/compare_views_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-


from django.test import TestCase
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse


from django.contrib.auth.models import User
from django.conf import settings
import os

class CompareViewsTestCase(TestCase):

def initialise(self):
""" Open files"""
self.rdf_file = open("examples/SPDXRdfExample-v2.0.rdf")
self.rdf_file2 = open("examples/SPDXRdfExample.rdf")
self.invalid_rdf = open("examples/SPDXRdfExample-v2.0_invalid.rdf")

def exit(self):
""" Close files"""
self.rdf_file.close()
self.rdf_file2.close()
self.invalid_rdf.close()

def test_compare(self):
"""GET Request for compare"""
if not settings.ANONYMOUS_LOGIN_ENABLED :
resp = self.client.get(reverse("compare"),follow=True,secure=True)
self.assertNotEqual(resp.redirect_chain,[])
self.assertIn(settings.LOGIN_URL, (i[0] for i in resp.redirect_chain))
self.assertEqual(resp.status_code,200)
self.client.force_login(User.objects.get_or_create(username='comparetestuser')[0])
resp2 = self.client.get(reverse("compare"),follow=True,secure=True)
self.assertEqual(resp2.status_code,200)
self.assertEqual(resp2.redirect_chain,[])
self.assertIn("app/compare.html",(i.name for i in resp2.templates))
self.assertEqual(resp2.resolver_match.func.__name__,"compare")
self.client.logout()

def test_compare_post_without_login(self):
"""POST Request for compare without login or ANONYMOUS_LOGIN_ENABLED==False """
if not settings.ANONYMOUS_LOGIN_ENABLED :
self.initialise()
resp = self.client.post(reverse("compare"),{'rfilename': "comparetest", 'files' : [self.rdf_file,self.rdf_file2]},follow=True,secure=True)
self.assertNotEqual(resp.redirect_chain,[])
self.assertIn(settings.LOGIN_URL, (i[0] for i in resp.redirect_chain))
self.assertEqual(resp.status_code,200)
self.exit()

def test_compare_post_without_file(self):
"""POST Request for compare without file upload"""
self.initialise()
self.client.force_login(User.objects.get_or_create(username='comparetestuser')[0])
resp = self.client.post(reverse("compare"),{'rfilename': "comparetest"},follow=True,secure=True)
self.assertEqual(resp.status_code,404)
self.assertTrue('error' in resp.context)
self.assertEqual(resp.redirect_chain,[])
self.exit()
self.client.logout()

def test_compare_post_with_one_file(self):
"""POST Request for compare with only one file"""
self.initialise()
self.client.force_login(User.objects.get_or_create(username='comparetestuser')[0])
resp = self.client.post(reverse("compare"),{'rfilename': "comparetest", 'files':[self.rdf_file,]},follow=True,secure=True)
self.assertEqual(resp.status_code,404)
self.assertTrue('error' in resp.context)
self.assertEqual(resp.redirect_chain,[])
self.exit()
self.client.logout()

def test_compare_two_rdf(self):
"""POST Request for comparing two rdf files"""
self.initialise()
self.client.force_login(User.objects.get_or_create(username='comparetestuser')[0])
resp = self.client.post(reverse("compare"),{'rfilename': 'comparetest','files': [self.rdf_file,self.rdf_file2]},follow=True,secure=True)
self.assertEqual(resp.status_code, 200)
self.assertIn("medialink",resp.context)
self.assertEqual(resp.redirect_chain,[])
self.assertTrue(resp.context["medialink"].startswith(settings.MEDIA_URL))
self.assertIn("Content-Type",resp.context)
self.assertEqual(resp.context["Content-Type"],"application/vnd.ms-excel")
self.exit()
self.client.logout()

def test_compare_invalid_rdf(self):
"""POST Request for comparing two files"""
self.initialise()
self.client.force_login(User.objects.get_or_create(username='comparetestuser')[0])
resp = self.client.post(reverse("compare"),{'rfilename': 'comparetest','files' : [self.rdf_file,self.invalid_rdf]},follow=True,secure=True)
self.assertEqual(resp.status_code, 400)
self.assertTrue('error' in resp.context)
self.assertEqual(resp.redirect_chain,[])
self.exit()
self.client.logout()
Loading