Bug 34448: Update cypress tests
[koha.git] / t / cypress / integration / ERM / Licenses_spec.ts
1 import { mount } from "@cypress/vue";
2 const dayjs = require("dayjs"); /* Cannot use our calendar JS code, it's in an include file (!)
3                                    Also note that moment.js is deprecated */
4
5 const dates = {
6     today_iso: dayjs().format("YYYY-MM-DD"),
7     today_us: dayjs().format("MM/DD/YYYY"),
8     tomorrow_iso: dayjs().add(1, "day").format("YYYY-MM-DD"),
9     tomorrow_us: dayjs().add(1, "day").format("MM/DD/YYYY"),
10 };
11 function get_license() {
12     return {
13         license_id: 1,
14         name: "license 1",
15         description: "my first license",
16         type: "local",
17         status: "active",
18         started_on: dates["today_iso"],
19         ended_on: dates["tomorrow_iso"],
20         user_roles: [],
21         documents: [
22             {
23                 license_id: 1,
24                 file_description: "file description",
25                 file_name: "file.json",
26                 notes: "file notes",
27                 physical_location: "file physical location",
28                 uri: "file uri",
29                 uploaded_on: "2022-10-27T11:57:02+00:00",
30             },
31         ],
32     };
33 }
34
35 describe("License CRUD operations", () => {
36     beforeEach(() => {
37         cy.login();
38         cy.title().should("eq", "Koha staff interface");
39         cy.intercept(
40             "GET",
41             "/cgi-bin/koha/svc/config/systempreferences/?pref=ERMModule",
42             '{"value":"1"}'
43         );
44         cy.intercept(
45             "GET",
46             "/cgi-bin/koha/svc/config/systempreferences/?pref=ERMProviders",
47             '{"value":"local"}'
48         );
49     });
50
51     it("List license", () => {
52         // GET license returns 500
53         cy.intercept("GET", "/api/v1/erm/licenses*", {
54             statusCode: 500,
55             error: "Something went wrong",
56         });
57         cy.visit("/cgi-bin/koha/erm/erm.pl");
58         cy.get("#navmenulist").contains("Licenses").click();
59         cy.get("main div[class='dialog alert']").contains(
60             /Something went wrong/
61         );
62
63         // GET licenses returns empty list
64         cy.intercept("GET", "/api/v1/erm/licenses*", []);
65         cy.visit("/cgi-bin/koha/erm/licenses");
66         cy.get("#licenses_list").contains("There are no licenses defined");
67
68         // GET licenses returns something
69         let license = get_license();
70         let licenses = [license];
71
72         cy.intercept("GET", "/api/v1/erm/licenses*", {
73             statusCode: 200,
74             body: licenses,
75             headers: {
76                 "X-Base-Total-Count": "1",
77                 "X-Total-Count": "1",
78             },
79         });
80         cy.intercept("GET", "/api/v1/erm/licenses/*", license);
81         cy.visit("/cgi-bin/koha/erm/licenses");
82         cy.get("#licenses_list").contains("Showing 1 to 1 of 1 entries");
83     });
84
85     it("Add license", () => {
86         // Click the button in the toolbar
87         cy.visit("/cgi-bin/koha/erm/licenses");
88         cy.contains("New license").click();
89         cy.get("#licenses_add h2").contains("New license");
90
91         // Fill in the form for normal attributes
92         let license = get_license();
93
94         cy.get("#licenses_add").contains("Submit").click();
95         cy.get("input:invalid,textarea:invalid,select:invalid").should(
96             "have.length",
97             4
98         );
99         cy.get("#license_name").type(license.name);
100         cy.get("#license_description").type(license.description);
101         cy.get("#licenses_add").contains("Submit").click();
102         cy.get("#license_type .vs__search").type(license.type + "{enter}", {
103             force: true,
104         });
105         cy.get("#license_status .vs__search").type(license.status + "{enter}", {
106             force: true,
107         });
108
109         cy.get("#started_on+input").click();
110         cy.get(".flatpickr-calendar")
111             .eq(0)
112             .find("span.today")
113             .click({ force: true });
114
115         cy.get("#ended_on+input").click();
116         cy.get(".flatpickr-calendar")
117             .eq(1)
118             .find("span.today")
119             .next("span")
120             .click();
121
122         // Add new document
123         cy.get("#documents").contains("Add new document").click();
124         cy.get("#document_0 input[id=file_0]").click();
125         cy.get("#document_0 input[id=file_0]").selectFile(
126             "t/cypress/fixtures/file.json"
127         );
128         cy.get("#document_0 .file_information span").contains("file.json");
129         cy.get("#document_0 input[id=file_description_0]").type(
130             "file description"
131         );
132         cy.get("#document_0 input[id=physical_location_0]").type(
133             "file physical location"
134         );
135         cy.get("#document_0 input[id=uri_0]").type("file URI");
136         cy.get("#document_0 input[id=notes_0]").type("file notes");
137
138         // Submit the form, get 500
139         cy.intercept("POST", "/api/v1/erm/licenses", {
140             statusCode: 500,
141             error: "Something went wrong",
142         });
143         cy.get("#licenses_add").contains("Submit").click();
144         cy.get("main div[class='dialog alert']").contains(
145             "Something went wrong: SyntaxError: Unexpected end of JSON input"
146         );
147
148         // Submit the form, success!
149         cy.intercept("POST", "/api/v1/erm/licenses", {
150             statusCode: 201,
151             body: license,
152         });
153         cy.get("#licenses_add").contains("Submit").click();
154         cy.get("main div[class='dialog message']").contains("License created");
155     });
156
157     it("Edit license", () => {
158         let license = get_license();
159         let licenses = [license];
160         // Click the 'Edit' button from the list
161         cy.intercept("GET", "/api/v1/erm/licenses*", {
162             statusCode: 200,
163             body: licenses,
164             headers: {
165                 "X-Base-Total-Count": "1",
166                 "X-Total-Count": "1",
167             },
168         });
169         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
170             "get-license"
171         );
172         cy.visit("/cgi-bin/koha/erm/licenses");
173         cy.get("#licenses_list table tbody tr:first").contains("Edit").click();
174         cy.wait("@get-license");
175         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
176         cy.get("#licenses_add h2").contains("Edit license");
177
178         // Form has been correctly filled in
179         cy.get("#license_name").should("have.value", license.name);
180         cy.get("#license_description").should(
181             "have.value",
182             license.description
183         );
184         cy.get("#license_type .vs__selected").contains("Local");
185         cy.get("#license_status .vs__selected").contains("Active");
186         cy.get("#started_on").invoke("val").should("eq", dates["today_iso"]);
187         cy.get("#ended_on").invoke("val").should("eq", dates["tomorrow_iso"]);
188
189         // Test related document
190         cy.get("#document_0 .file_information span").contains("file.json");
191
192         // Submit the form, get 500
193         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
194             statusCode: 500,
195             error: "Something went wrong",
196         });
197         cy.get("#licenses_add").contains("Submit").click();
198         cy.get("main div[class='dialog alert']").contains(
199             "Something went wrong: SyntaxError: Unexpected end of JSON input"
200         );
201
202         // Submit the form, success!
203         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
204             statusCode: 200,
205             body: license,
206         });
207         cy.get("#licenses_add").contains("Submit").click();
208         cy.get("main div[class='dialog message']").contains("License updated");
209     });
210
211     it("Show license", () => {
212         let license = get_license();
213         let licenses = [license];
214         // Click the "name" link from the list
215         cy.intercept("GET", "/api/v1/erm/licenses*", {
216             statusCode: 200,
217             body: licenses,
218             headers: {
219                 "X-Base-Total-Count": "1",
220                 "X-Total-Count": "1",
221             },
222         });
223         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
224             "get-license"
225         );
226         cy.visit("/cgi-bin/koha/erm/licenses");
227         let name_link = cy.get(
228             "#licenses_list table tbody tr:first td:first a"
229         );
230         name_link.should(
231             "have.text",
232             license.name + " (#" + license.license_id + ")"
233         );
234         name_link.click();
235         cy.wait("@get-license");
236         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
237         cy.get("#licenses_show h2").contains("License #" + license.license_id);
238     });
239
240     it("Delete license", () => {
241         let license = get_license();
242         let licenses = [license];
243
244         // Click the 'Delete' button from the list
245         cy.intercept("GET", "/api/v1/erm/licenses*", {
246             statusCode: 200,
247             body: licenses,
248             headers: {
249                 "X-Base-Total-Count": "1",
250                 "X-Total-Count": "1",
251             },
252         });
253         cy.intercept("GET", "/api/v1/erm/licenses/*", license);
254         cy.visit("/cgi-bin/koha/erm/licenses");
255
256         cy.get("#licenses_list table tbody tr:first")
257             .contains("Delete")
258             .click();
259         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
260         cy.contains(license.name);
261
262         // Accept the confirmation dialog, get 500
263         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
264             statusCode: 500,
265             error: "Something went wrong",
266         });
267         cy.contains("Yes, delete").click();
268         cy.get("main div[class='dialog alert']").contains(
269             "Something went wrong: SyntaxError: Unexpected end of JSON input"
270         );
271
272         // Accept the confirmation dialog, success!
273         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
274             statusCode: 204,
275             body: null,
276         });
277         cy.get("#licenses_list table tbody tr:first")
278             .contains("Delete")
279             .click();
280         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
281         cy.contains("Yes, delete").click();
282         cy.get("main div[class='dialog message']")
283             .contains("License")
284             .contains("deleted");
285
286         // Delete from show
287         // Click the "name" link from the list
288         cy.intercept("GET", "/api/v1/erm/licenses*", {
289             statusCode: 200,
290             body: licenses,
291             headers: {
292                 "X-Base-Total-Count": "1",
293                 "X-Total-Count": "1",
294             },
295         });
296         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
297             "get-license"
298         );
299         cy.visit("/cgi-bin/koha/erm/licenses");
300         let name_link = cy.get(
301             "#licenses_list table tbody tr:first td:first a"
302         );
303         name_link.should(
304             "have.text",
305             license.name + " (#" + license.license_id + ")"
306         );
307         name_link.click();
308         cy.wait("@get-license");
309         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
310         cy.get("#licenses_show h2").contains("License #" + license.license_id);
311
312         cy.get("#licenses_show .action_links .fa-trash").click();
313         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
314         cy.contains("Yes, delete").click();
315
316         //Make sure we return to list after deleting from show
317         cy.get("#licenses_list table tbody tr:first");
318     });
319 });