Skip to content
Snippets Groups Projects
Commit d7a76ebf authored by Dominik Seeger's avatar Dominik Seeger Committed by robinwilliam.hundt
Browse files

Added first tests for AutoLogout component

parent 85378627
Branches
Tags
1 merge request!128Merge improve testing
......@@ -97,7 +97,7 @@ class AssignmentApiViewSet(
@permission_classes((IsReviewer,))
def list(self, *args, **kwargs):
super().list(*args, **kwargs)
return super().list(*args, **kwargs)
@action(detail=False, permission_classes=(IsReviewer,), methods=['get', 'delete'])
def active(self, request):
......
......@@ -15,10 +15,12 @@
</v-card-text>
<v-card-actions>
<v-btn flat color="grey lighten-0"
id="logout-btn"
@click="logout"
>Logout now</v-btn>
<v-spacer/>
<v-btn flat color="blue darken-2"
id="continue-btn"
@click="continueWork"
>Continue</v-btn>
</v-card-actions>
......
import Vue from 'vue'
import * as hljs from 'highlight.js'
import * as api from '@/api'
import { nameSpacer } from '@/util/helpers'
import { Feedback, FeedbackComment, Submission, SubmissionNoType } from '@/models'
import { Feedback, FeedbackComment, SubmissionNoType } from '@/models'
import { RootState } from '@/store/store'
import { Module } from 'vuex'
import { getStoreBuilder, BareActionContext } from 'vuex-typex'
export const subNotesNamespace = nameSpacer('submissionNotes/')
export interface SubmissionNotesState {
submission: SubmissionNoType
ui: {
......@@ -143,13 +139,16 @@ async function submitFeedback ({ state }: BareActionContext<SubmissionNotesState
}
if (state.origFeedback.score === undefined && state.updatedFeedback.score === undefined) {
throw new Error('You need to give a score.')
} else if (state.updatedFeedback.score !== undefined) {
feedback.score = state.updatedFeedback.score
} else {
feedback.score = state.updatedFeedback.score || state.origFeedback.score || 0
feedback.score = state.origFeedback.score
}
if (Object.keys(state.updatedFeedback.feedbackLines || {}).length > 0) {
feedback.feedbackLines = state.updatedFeedback.feedbackLines
} else if (feedback.score! < SubmissionNotes.submissionType.fullScore!) {
throw new Error('You need to provide a reason for a reduced score.')
throw new Error('You need to add or change a comment when setting a non full score.')
}
// delete those comments that have been marked for deletion
await SubmissionNotes.deleteComments()
......
import Vuex from 'vuex'
import { mount, createLocalVue } from '@vue/test-utils'
import AutoLogout from '../../src/components/AutoLogout.vue'
import AutoLogout from '@/components/AutoLogout.vue'
import sinon from 'sinon'
import { Authentication } from '@/store/modules/authentication'
import chai from 'chai'
chai.should()
const localVue = createLocalVue();
localVue.use(Vuex);
let localVue = createLocalVue()
localVue.use(Vuex)
describe('Auto Logout Unit Tests', () => {
let store: any = null
let consoleTemp = {
warn: console.warn,
error: console.error,
}
before(function() {
console.warn = function() {}
console.error = function() {}
})
after(function() {
console.warn = consoleTemp.warn
console.error = consoleTemp.error
})
beforeEach(() => {
store = new Vuex.Store({
state: {}
})
})
afterEach(() => {
sinon.restore()
})
it('should be hidden by default', () => {
let store = new Vuex.Store({})
let wrapper = mount(AutoLogout, { localVue, store })
let wrapper = mount(AutoLogout, { localVue: localVue, store })
wrapper.vm.$data.logoutDialog.should.equal(false)
wrapper.html().should.contain('<div class="v-dialog v-dialog--persistent" style="max-width: 30%; display: none;">')
})
it('should be visible when logoutDialog is set to true', () => {
let store = new Vuex.Store({})
let wrapper = mount(AutoLogout, { localVue, store })
wrapper.vm.$data.logoutDialog = true
wrapper.html().should.contain('<div class="v-dialog v-dialog--active v-dialog--persistent" style="max-width: 30%;">')
})
it('should get a refresh token from the server when user clicks button', () => {
let spy = sinon.spy()
sinon.replace(Authentication, 'refreshJWT', spy)
let store = new Vuex.Store({})
let wrapper = mount(AutoLogout, { localVue, store })
// @ts-ignore
wrapper.vm.continueWork()
wrapper.find('#continue-btn').trigger('click')
spy.called.should.equal(true)
})
})
......@@ -93,8 +93,9 @@ class ExportTestModal(LiveServerTestCase):
export_type_json = data_export_modal.find_element_by_xpath("//*[contains(text(), 'JSON')]")
export_type_json.click()
data_export_btn = data_export_modal.find_element_by_id('export-data-download-btn')
before_click_handles = self.browser.window_handles
data_export_btn.click()
WebDriverWait(self.browser, 10).until(ec.new_window_is_opened)
WebDriverWait(self.browser, 10).until(ec.new_window_is_opened(before_click_handles))
tabs = self.browser.window_handles
self.assertEqual(2, len(tabs))
self.browser.switch_to.window(tabs[1])
......@@ -109,9 +110,10 @@ class ExportTestModal(LiveServerTestCase):
export_instance.click()
instance_export_modal = self.browser.find_element_by_id('instance-export-modal')
instance_export_btn = instance_export_modal.find_element_by_id('instance-export-dl')
before_click_handles = self.browser.window_handles
instance_export_btn.click()
WebDriverWait(self.browser, 10).until(ec.new_window_is_opened)
tabs = self.browser.window_handles
self.assertEqual(2, len(tabs))
self.browser.switch_to.window(tabs[1])
WebDriverWait(self.browser, 10).until(ec.new_window_is_opened(before_click_handles))
after_click_handles = self.browser.window_handles
self.assertEqual(2, len(after_click_handles))
self.browser.switch_to.window(after_click_handles[1])
self.assertIn('B.Inf.4242 Test Module', self.browser.find_element_by_tag_name('body').text)
import time
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from core import models
from core.models import UserAccount
......@@ -47,11 +46,20 @@ class UntestedParent:
self.assertEqual('Statistics', title.text)
def test_available_tasks_are_shown(self):
# A function that takes the element corresponding to the tasks
# component and return a function that can be used as a condition for
# WebDriverWait
def subscriptions_loaded_cond(tasks_el):
def loaded(*args):
sub_links = tasks_el.find_elements_by_tag_name('a')
return any((link != '/home' for link in extract_hrefs_hashes(sub_links)))
return loaded
self._login()
tasks = self.browser.find_element_by_name('subscription-list')
title = tasks.find_element_by_class_name('v-toolbar__title')
self.assertEqual('Tasks', title.text)
time.sleep(8)
WebDriverWait(self.browser, 6).until(subscriptions_loaded_cond(tasks))
subscription_links = extract_hrefs_hashes(
tasks.find_elements_by_tag_name('a')
)
......
import time
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from core.models import UserAccount
from util.factories import make_test_data
......@@ -102,7 +102,7 @@ class LoginPageTest(LiveServerTestCase):
password_input = self.browser.find_element_by_xpath('//input[@aria-label="Password"]')
password_input.send_keys('p')
self.browser.find_element_by_xpath('//button[@type="submit"]').send_keys(Keys.ENTER)
time.sleep(1)
WebDriverWait(self.browser, 3).until(ec.url_contains('/home'))
def test_tutor_can_login(self):
tutor = self.test_data['tutors'][0]
......@@ -130,8 +130,9 @@ class LoginPageTest(LiveServerTestCase):
username_input.send_keys(username)
password_input = self.browser.find_element_by_id('input-register-password')
password_input.send_keys(password)
self.browser.find_element_by_id('register-submit').click()
time.sleep(1)
register_submit_el = self.browser.find_element_by_id('register-submit')
register_submit_el.click()
WebDriverWait(self.browser, 3).until_not(ec.visibility_of(register_submit_el))
tutor = UserAccount.objects.get(username=username)
self.assertEqual(UserAccount.TUTOR, tutor.role)
self.assertFalse(tutor.is_active, "Tutors should be inactive after registered")
......@@ -6,6 +6,8 @@ from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
def create_browser() -> webdriver.Firefox:
......@@ -23,6 +25,7 @@ def login(browser, live_server_url, username, password='p'):
password_input = browser.find_element_by_xpath('//input[@aria-label="Password"]')
password_input.send_keys(password)
browser.find_element_by_xpath('//button[@type="submit"]').send_keys(Keys.ENTER)
WebDriverWait(browser, 3).until(ec.url_contains('/home'))
def reset_browser_after_test(browser: webdriver.Firefox, live_server_url):
......
from .default import *
from .instance import *
from .live import *
REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['anon'] = '1000/minute'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment