Skip to content

Commit

Permalink
Merge branch 'development' into permission_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
luisza committed Jan 2, 2024
2 parents 1f55193 + 014d5cd commit e2c2a2a
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 7 deletions.
Binary file added docs/source/_static/gif/create_org.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions fixtures/selenium/organization_manage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "adminpass",
"last_login": "2023-01-10T22:49:30.603Z",
"is_superuser": true,
"username": "admin",
"first_name": "Organilab",
"last_name": "Admin",
"email": "[email protected]",
"is_staff": false,
"is_active": true,
"date_joined": "2018-07-09T19:45:29Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth_and_perms.profile",
"pk": 1,
"fields": {
"user": 1,
"phone_number": "8888-8888",
"id_card": "0-0000-0000",
"job_position": "Administrator",
"laboratories": [
]
}
}
]
Empty file.
54 changes: 54 additions & 0 deletions src/laboratory/tests/selenium_tests/test_create_org.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.contrib.auth.models import User
from django.urls import reverse
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from organilab_test.tests.base import SeleniumBase
from django.utils.translation import gettext_lazy as _


class OrganizationSeleniumTest(SeleniumBase):
fixtures = ["selenium/organization_manage.json"]

def setUp(self):
super().setUp()
self.user = User.objects.get(pk=1)
self.select_org_url = self.live_server_url + str(reverse('auth_and_perms:select_organization_by_user'))
self.force_login(user=self.user, driver=self.selenium, base_url=self.live_server_url)
self.folder_name = "create_org"
self.create_directory_path(folder_name=self.folder_name)

def test_create_organization(self):
self.screenShots("1_select_org", time_out=3)
action = ActionChains(self.selenium)

org_and_perms = self.selenium.find_element(By.XPATH, ".//ul[@class='nav side-menu']/li[2]/a")
action.move_to_element(org_and_perms).perform()
org_and_perms.click()
self.screenShots("2_org_and_perms", time_out=3)

manage_orgs = self.selenium.find_element(By.XPATH, ".//ul[@class='nav side-menu']/li[2]/ul/li/a")
action.move_to_element(manage_orgs).perform()
manage_orgs.click()
self.screenShots("3_manage_orgs", time_out=3)

btn_add_org = self.selenium.find_element(By.XPATH, ".//span[@class='addOrgStructureEmpty']")
action.move_to_element(btn_add_org).perform()
btn_add_org.click()
self.screenShots("4_btn_add_org", time_out=3)

name_input = self.selenium.find_element(By.XPATH, ".//div[@id='addOrganizationmodal']/div/div[@class='modal-content']/form/div[@class='modal-body']/div/div/input[@id='id_name']")

self.screenShots("5_add_org_form", time_out=3)
action.move_to_element(name_input).perform()
name_input.click()
self.screenShots("6_name_input_click", time_out=3)

name_input.send_keys(_("Organization Name"))
self.screenShots("7_name_input_set_value", time_out=3)

btn_create_org = self.selenium.find_element(By.XPATH, ".//div[@id='addOrganizationmodal']/div/div[@class='modal-content']/form/div[@class='modal-footer']/button[@type='submit']")
action.move_to_element(btn_create_org).perform()
btn_create_org.click()
self.screenShots("8_btn_create_org_click", time_out=3)

self.create_gifs(self.dir, self.folder_name)
2 changes: 2 additions & 0 deletions src/organilab/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@

FIXTURE_DIRS = os.getenv('FIXTURE_DIRS', str(BASE_DIR.parent/ 'fixtures/') ).split(',')

DOCS_SOURCE_DIR = os.getenv('DOCS_STATIC_DIR', str(BASE_DIR.parent / 'docs/source/'))

# Authentication settings
LOGIN_REDIRECT_URL = reverse_lazy('auth_and_perms:select_organization_by_user')
LOGOUT_REDIRECT_URL = reverse_lazy('index')
Expand Down
6 changes: 0 additions & 6 deletions src/organilab_test/tests.py

This file was deleted.

Empty file.
122 changes: 122 additions & 0 deletions src/organilab_test/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import shutil
from importlib import import_module
from django.contrib.auth import SESSION_KEY, BACKEND_SESSION_KEY, HASH_SESSION_KEY
from PIL import Image
import glob
from selenium import webdriver
from pathlib import Path
from Screenshot import Screenshot
from django.conf import settings
from time import sleep
from django.contrib.staticfiles.testing import StaticLiveServerTestCase


class SeleniumBase(StaticLiveServerTestCase):

@classmethod
def setUpClass(cls):
super(SeleniumBase, cls).setUpClass()

cls.timeout = 30

cls.ob = Screenshot.Screenshot()
cls.options = webdriver.FirefoxOptions()

cls.selenium = webdriver.Chrome()
cls.selenium.maximize_window()

cls.tmp = Path(settings.BASE_DIR) / 'tmp'
cls.folder = '%s/%dx%d' % (cls.tmp, 120, 200)
cls.dir = Path(settings.BASE_DIR) / cls.folder
cls.static_save_path = Path(settings.DOCS_SOURCE_DIR) / '_static'
cls.save_path_gif = cls.static_save_path / 'gif'

if not cls.tmp.exists():
cls.tmp.mkdir()


def create_directory_path(self, time_out=10, url=None, folder_name=""):
# Folder name is an specific name by action, for example 'create_org', 'view_org_users', etc

if url:
self.selenium.get(url)

self.folder = '%s/%dx%d' % (
self.tmp, 120, 100)
self.dir = Path(settings.BASE_DIR) / self.folder

if not self.dir.exists():
self.dir.mkdir()

self.dir = self.dir / folder_name

if not self.dir.exists():
self.dir.mkdir()



def screenShots(self, name, time_out, save_screenshot=False):
x_Full_Page = '%s.png' % name
sleep(time_out)
self.selenium.save_screenshot(
str(Path(self.dir / x_Full_Page).absolute().resolve()))

if save_screenshot:
self.selenium.save_screenshot(
str(Path(self.static_save_path / x_Full_Page).absolute().resolve()))

def create_gifs(self, file_url, folder):
images = []

# get all the images in the 'images for gif' folder
for filename in sorted(glob.glob(
'{}/*.png'.format(file_url))): # loop through all png files in the folder
im = Image.open(filename) # open the image
im_small = im.resize((1440, 700),
resample=0) # resize them to make them a bit smaller
images.append(im_small) # add the image to the list

# calculate the frame number of the last frame (ie the number of images)
last_frame = (len(images))

# create 10 extra copies of the last frame (to make the gif spend longer on the most recent data)
for x in range(0, 9):
im = images[last_frame - 1]
images.append(im)

# save as a gif
gif_name = '/%s%s' %(folder, '.gif')
images[0].save(str(self.save_path_gif) + gif_name,
save_all=True, append_images=images[1:], optimize=False,
duration=1500, loop=0)

def force_login(self, user, driver, base_url):
from django.conf import settings
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
driver.get(base_url)

session = SessionStore()
session[SESSION_KEY] = user._meta.pk.value_to_string(user)
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()

cookie = {
'name': settings.SESSION_COOKIE_NAME,
'value': session.session_key,
'path': '/'
}
driver.add_cookie(cookie)
driver.refresh()

@classmethod
def tearDownClass(cls):
cls.selenium.quit()

#Removing tmp directory
try:
shutil.rmtree(cls.tmp)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))

super(SeleniumBase, cls).tearDownClass()
3 changes: 2 additions & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pylint-django
pylint
virtualenv==20.24.4
pyproject-api==1.5.4

selenium==4.5.0
selenium-screenshot==2.0.0

0 comments on commit e2c2a2a

Please sign in to comment.