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