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