This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
forked from msdlt/canvas-where-am-I
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpn-empty-course.test.js
139 lines (120 loc) · 4.87 KB
/
cpn-empty-course.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const axios = require('axios');
const assert = require('assert');
const dotenv = require('dotenv');
dotenv.config();
jest.setTimeout(1200000);
// Contains the created course, reusable between tests.
let courseObject = {};
// Configuration parameters, see .env.example for more information.
const token = process.env.OAUTH_TOKEN;
const host = process.env.CANVAS_HOST;
const account = process.env.ACCOUNT_ID;
getRandomModule = () => {
return moduleArray[Math.floor(Math.random() * moduleArray.length)];
}
goToCourse = async (page) => {
await page.goto(`${host}/courses/${courseObject.id}`);
}
appendCPNScript = async (page) => {
await page.evaluate(() => ENV['FORCE_CPN'] = true);
await Promise.all([
page.addScriptTag({ path: './canvas-where-am-I.js' }),
page.addStyleTag({ path: './canvas-where-am-I.css' }),
]);
}
describe('Test the CPN script logic with an empty course.', () => {
beforeAll(async () => {
assert(token, 'You must set the environmental variable OAUTH_TOKEN');
assert(host, 'You must set the environmental variable CANVAS_HOST');
assert(account, 'You must set the environmental variable ACCOUNT_ID');
// Creates an empty course in the instance to check the navigation.
const course = { course: { name: 'IGNORE: CPN EMPTY TESTING', course_code: 'ignore_cpn_empty_testing', default_view: 'modules' } };
await axios({
method: 'POST',
url: `${host}/api/v1/accounts/${account}/courses`,
headers: {'Authorization': 'Bearer ' + token},
data: course
}).then((response) => {
courseObject = response.data;
console.log('course created');
}).catch(err => {
console.log('error creating course: ', err);
throw err;
});
});
afterAll(async () => {
// Delete the course
await axios({
method: 'DELETE',
url: `${host}/api/v1/courses/${courseObject.id}`,
headers: {'Authorization': 'Bearer ' + token},
data: { event: 'delete' }
})
});
beforeEach(async () => {
// We should always have more than 60 seconds as we sometimes see a 60 second stall.
await page.setDefaultTimeout(90000);
await Promise.all([
page.waitForNavigation(),
axios.get(`${host}/login/session_token`, {headers: {'Authorization': 'Bearer ' + token}})
.then((response) => {
return page.goto(response.data.session_url);
})
]);
});
it('General: Check the course is created and navigable.', async () => {
await goToCourse(page);
await expect(page.title()).resolves.toMatch(courseObject.name);
});
it('Tile View: Check content DIV exists.', async () => {
await goToCourse(page);
const element = await page.$('#content');
await expect(element).not.toBeNull();
});
it('Tile View: Check the script does not make any changes in the home page.', async () => {
await goToCourse(page);
await appendCPNScript(page);
// Check the message that there are no courses
const noModulesMessage = await page.$('#no_context_modules_message');
await expect(noModulesMessage).not.toBeNull();
// Check the course_home_content div is not removed
const divHomeContent = await page.$('#course_home_content');
await expect(divHomeContent).not.toBeNull();
// Check the module_nav div does not exist
const moduleNav = await page.$('#module_nav');
await expect(moduleNav).toBeNull();
// Check the ability to add modules
const addModuleLink = await page.$('.add_module_link');
await expect(addModuleLink).not.toBeNull();
});
it('Tile View: Ensure the script does not perform any action in other course homes.', async () => {
// Replaces the course home by the feed instead of modules.
await axios({
method: 'PUT',
url: `${host}/api/v1/courses/${courseObject.id}`,
headers: {'Authorization': 'Bearer ' + token},
data: 'course[default_view]=feed'
});
await goToCourse(page);
await appendCPNScript(page);
// Check the home page is feed and not modules
const noModulesMessage = await page.$('#no_context_modules_message');
await expect(noModulesMessage).toBeNull();
const recentActivityElement = await page.$('.recent_activity');
await expect(recentActivityElement).not.toBeNull();
// Check the course_home_content div is not removed
const divHomeContent = await page.$('#course_home_content');
await expect(divHomeContent).not.toBeNull();
// Check the module_nav div does not exist
const moduleNav = await page.$('#module_nav');
await expect(moduleNav).toBeNull();
});
it('Modules submenu: Check the script does not make any changes in LHS menu.', async () => {
await goToCourse(page);
await appendCPNScript(page);
const modulesToolLink = await page.$$('li.section a.modules');
await expect(modulesToolLink).not.toBeNull();
const submenuElement = await page.$('.ou-section-tabs-sub');
await expect(submenuElement).toBeNull();
});
});