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             "/api/v1/erm/config",
42             '{"settings":{"ERMModule":"1","ERMProviders":["local"]}}'
43         );
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}", {
98             force: true,
99         });
100         cy.get("#license_status .vs__search").type(license.status + "{enter}", {
101             force: true,
102         });
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(
121             "t/cypress/fixtures/file.json"
122         );
123         cy.get("#document_0 .file_information span").contains("file.json");
124         cy.get("#document_0 input[id=file_description_0]").type(
125             "file description"
126         );
127         cy.get("#document_0 input[id=physical_location_0]").type(
128             "file physical location"
129         );
130         cy.get("#document_0 input[id=uri_0]").type("file URI");
131         cy.get("#document_0 input[id=notes_0]").type("file notes");
132
133         // Submit the form, get 500
134         cy.intercept("POST", "/api/v1/erm/licenses", {
135             statusCode: 500,
136             error: "Something went wrong",
137         });
138         cy.get("#licenses_add").contains("Submit").click();
139         cy.get("main div[class='dialog alert']").contains(
140             "Something went wrong: SyntaxError: Unexpected end of JSON input"
141         );
142
143         // Submit the form, success!
144         cy.intercept("POST", "/api/v1/erm/licenses", {
145             statusCode: 201,
146             body: license,
147         });
148         cy.get("#licenses_add").contains("Submit").click();
149         cy.get("main div[class='dialog message']").contains("License created");
150     });
151
152     it("Edit license", () => {
153         let license = get_license();
154         let licenses = [license];
155         // Click the 'Edit' button from the list
156         cy.intercept("GET", "/api/v1/erm/licenses*", {
157             statusCode: 200,
158             body: licenses,
159             headers: {
160                 "X-Base-Total-Count": "1",
161                 "X-Total-Count": "1",
162             },
163         });
164         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
165             "get-license"
166         );
167         cy.visit("/cgi-bin/koha/erm/licenses");
168         cy.get("#licenses_list table tbody tr:first").contains("Edit").click();
169         cy.wait("@get-license");
170         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
171         cy.get("#licenses_add h2").contains("Edit license");
172
173         // Form has been correctly filled in
174         cy.get("#license_name").should("have.value", license.name);
175         cy.get("#license_description").should(
176             "have.value",
177             license.description
178         );
179         cy.get("#license_type .vs__selected").contains("Local");
180         cy.get("#license_status .vs__selected").contains("Active");
181         cy.get("#started_on").invoke("val").should("eq", dates["today_iso"]);
182         cy.get("#ended_on").invoke("val").should("eq", dates["tomorrow_iso"]);
183
184         // Test related document
185         cy.get("#document_0 .file_information span").contains("file.json");
186
187         // Submit the form, get 500
188         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
189             statusCode: 500,
190             error: "Something went wrong",
191         });
192         cy.get("#licenses_add").contains("Submit").click();
193         cy.get("main div[class='dialog alert']").contains(
194             "Something went wrong: SyntaxError: Unexpected end of JSON input"
195         );
196
197         // Submit the form, success!
198         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
199             statusCode: 200,
200             body: license,
201         });
202         cy.get("#licenses_add").contains("Submit").click();
203         cy.get("main div[class='dialog message']").contains("License updated");
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("License #" + license.license_id);
233     });
234
235     it("Delete license", () => {
236         let license = get_license();
237         let licenses = [license];
238
239         // Click the 'Delete' button from the list
240         cy.intercept("GET", "/api/v1/erm/licenses*", {
241             statusCode: 200,
242             body: licenses,
243             headers: {
244                 "X-Base-Total-Count": "1",
245                 "X-Total-Count": "1",
246             },
247         });
248         cy.intercept("GET", "/api/v1/erm/licenses/*", license);
249         cy.visit("/cgi-bin/koha/erm/licenses");
250
251         cy.get("#licenses_list table tbody tr:first")
252             .contains("Delete")
253             .click();
254         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
255         cy.contains(license.name);
256
257         // Accept the confirmation dialog, get 500
258         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
259             statusCode: 500,
260             error: "Something went wrong",
261         });
262         cy.contains("Yes, delete").click();
263         cy.get("main div[class='dialog alert']").contains(
264             "Something went wrong: SyntaxError: Unexpected end of JSON input"
265         );
266
267         // Accept the confirmation dialog, success!
268         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
269             statusCode: 204,
270             body: null,
271         });
272         cy.get("#licenses_list table tbody tr:first")
273             .contains("Delete")
274             .click();
275         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
276         cy.contains("Yes, delete").click();
277         cy.get("main div[class='dialog message']")
278             .contains("License")
279             .contains("deleted");
280
281         // Delete from show
282         // Click the "name" link from the list
283         cy.intercept("GET", "/api/v1/erm/licenses*", {
284             statusCode: 200,
285             body: licenses,
286             headers: {
287                 "X-Base-Total-Count": "1",
288                 "X-Total-Count": "1",
289             },
290         });
291         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
292             "get-license"
293         );
294         cy.visit("/cgi-bin/koha/erm/licenses");
295         let name_link = cy.get(
296             "#licenses_list table tbody tr:first td:first a"
297         );
298         name_link.should(
299             "have.text",
300             license.name + " (#" + license.license_id + ")"
301         );
302         name_link.click();
303         cy.wait("@get-license");
304         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
305         cy.get("#licenses_show h2").contains("License #" + license.license_id);
306
307         cy.get("#licenses_show .action_links .fa-trash").click();
308         cy.get(".dialog.alert.confirmation h1").contains("remove this license");
309         cy.contains("Yes, delete").click();
310
311         //Make sure we return to list after deleting from show
312         cy.get("#licenses_list table tbody tr:first");
313     });
314 });