From bb50f586a37bbecf72b841e900a4ae3c2d0bbe11 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 11 Aug 2023 13:16:48 +0100 Subject: [PATCH 1/4] test: [#226] E2E test: delete category The category list is reloaded from the API before mounting the admin settings tab for categories. It's needed becuase we insert the category directly in the database no via the UI. --- cypress.config.ts | 18 ++++-- cypress/e2e/contexts/category/commands.ts | 4 ++ cypress/e2e/contexts/category/fixtures.ts | 7 ++ cypress/e2e/contexts/category/specs/add.cy.ts | 2 - .../e2e/contexts/category/specs/delete.cy.ts | 64 +++++++++++++++++++ cypress/e2e/contexts/category/tasks.ts | 17 +++++ cypress/support/commands.ts | 1 + pages/admin/settings/categories.vue | 10 ++- 8 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 cypress/e2e/contexts/category/fixtures.ts create mode 100644 cypress/e2e/contexts/category/specs/delete.cy.ts diff --git a/cypress.config.ts b/cypress.config.ts index 26d306c5..0297ae27 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from "cypress"; import { grantAdminRole, deleteUser } from "./cypress/e2e/contexts/user/tasks"; import { deleteTorrent } from "./cypress/e2e/contexts/torrent/tasks"; -import { deleteCategory } from "./cypress/e2e/contexts/category/tasks"; +import { deleteCategory, addCategory } from "./cypress/e2e/contexts/category/tasks"; import { DatabaseConfig } from "./cypress/e2e/common/database"; function databaseConfig (config: Cypress.PluginConfigOptions): DatabaseConfig { @@ -15,17 +15,23 @@ export default defineConfig({ baseUrl: "http://localhost:3000", setupNodeEvents (on, config) { on("task", { - grantAdminRole: ({ username }) => { - return grantAdminRole(username, databaseConfig(config)); + // Category context + deleteCategory: ({ name }) => { + return deleteCategory(name, databaseConfig(config)); + }, + addCategory: ({ name }) => { + return addCategory(name, databaseConfig(config)); }, + // Torrent context deleteTorrent: ({ infohash }) => { return deleteTorrent(infohash, databaseConfig(config)); }, + // User context + grantAdminRole: ({ username }) => { + return grantAdminRole(username, databaseConfig(config)); + }, deleteUser: ({ username }) => { return deleteUser(username, databaseConfig(config)); - }, - deleteCategory: ({ name }) => { - return deleteCategory(name, databaseConfig(config)); } }); } diff --git a/cypress/e2e/contexts/category/commands.ts b/cypress/e2e/contexts/category/commands.ts index 9bc06db0..f6429725 100644 --- a/cypress/e2e/contexts/category/commands.ts +++ b/cypress/e2e/contexts/category/commands.ts @@ -3,3 +3,7 @@ Cypress.Commands.add("delete_category", (name) => { cy.task("deleteCategory", { name }); }); + +Cypress.Commands.add("add_category", (name) => { + cy.task("addCategory", { name }); +}); diff --git a/cypress/e2e/contexts/category/fixtures.ts b/cypress/e2e/contexts/category/fixtures.ts new file mode 100644 index 00000000..cbde8076 --- /dev/null +++ b/cypress/e2e/contexts/category/fixtures.ts @@ -0,0 +1,7 @@ +export function random_category_name (): string { + return `category-${random_category_id()}`; +} + +function random_category_id (): number { + return Math.floor(Math.random() * 1000000); +} diff --git a/cypress/e2e/contexts/category/specs/add.cy.ts b/cypress/e2e/contexts/category/specs/add.cy.ts index dd244657..0c9d85a4 100644 --- a/cypress/e2e/contexts/category/specs/add.cy.ts +++ b/cypress/e2e/contexts/category/specs/add.cy.ts @@ -50,7 +50,6 @@ describe("A non admin authenticated user", () => { it("should not be able to add a new category", () => { cy.visit("/admin/settings/categories"); - cy.contains("Please login to manage admin settings."); }); }); @@ -58,7 +57,6 @@ describe("A non admin authenticated user", () => { describe("A guest user", () => { it("should not be able to add a new category", () => { cy.visit("/admin/settings/categories"); - cy.contains("Please login to manage admin settings."); }); }); diff --git a/cypress/e2e/contexts/category/specs/delete.cy.ts b/cypress/e2e/contexts/category/specs/delete.cy.ts new file mode 100644 index 00000000..1e7c97b6 --- /dev/null +++ b/cypress/e2e/contexts/category/specs/delete.cy.ts @@ -0,0 +1,64 @@ +import { RegistrationForm, random_user_registration_data } from "../../user/registration"; +import { random_category_name } from "../fixtures"; + +describe("The admin user", () => { + let registration_form: RegistrationForm; + + before(() => { + registration_form = random_user_registration_data(); + cy.register_as_admin_and_login(registration_form); + }); + + after(() => { + cy.delete_user(registration_form.username); + }); + + it("should be able to delete a category", () => { + const category_name = random_category_name(); + + cy.add_category(category_name); + + // Go to admin settings + cy.get("div[data-cy=\"user-menu\"]").click(); + cy.get("li[data-cy=\"admin-settings-link\"]").click(); + + // Click categories tab + cy.contains("a", "categories").click(); + + // Delete the category + cy.get(`button[data-cy="delete-category-${category_name}"]`).click(); + + cy.on("window:confirm", (str) => { + expect(str).to.equal(`Are you sure you want to delete ${category_name}?`); + }); + + cy.on("window:confirm", () => true); + + cy.get(`[data-cy="delete-category-${category_name}"]`).should("not.exist"); + }); +}); + +describe("A non admin authenticated user", () => { + let registration_form: RegistrationForm; + + before(() => { + registration_form = random_user_registration_data(); + cy.register_and_login(registration_form); + }); + + after(() => { + cy.delete_user(registration_form.username); + }); + + it("should not be able to delete category", () => { + cy.visit("/admin/settings/categories"); + cy.contains("Please login to manage admin settings."); + }); +}); + +describe("A guest user", () => { + it("should not be able to delete a category", () => { + cy.visit("/admin/settings/categories"); + cy.contains("Please login to manage admin settings."); + }); +}); diff --git a/cypress/e2e/contexts/category/tasks.ts b/cypress/e2e/contexts/category/tasks.ts index 4273bdae..3ff4660f 100644 --- a/cypress/e2e/contexts/category/tasks.ts +++ b/cypress/e2e/contexts/category/tasks.ts @@ -12,6 +12,16 @@ export const deleteCategory = async (name: string, db_config: DatabaseConfig): P } }; +// Task to add a new category +export const addCategory = async (name: string, db_config: DatabaseConfig): Promise => { + try { + const result = await runDatabaseQuery(addCategoryQuery(name), db_config); + return name; + } catch (err) { + return await Promise.reject(err); + } +}; + // Database query specifications function deleteCategoryQuery (name: string): DatabaseQuery { @@ -20,3 +30,10 @@ function deleteCategoryQuery (name: string): DatabaseQuery { params: [name] }; } + +function addCategoryQuery (name: string): DatabaseQuery { + return { + query: "INSERT INTO torrust_categories (name) VALUES (?)", + params: [name] + }; +} diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 506f37fd..9a7f3099 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -22,6 +22,7 @@ declare global { delete_torrent(torrent_info: TestTorrentInfo, infohash: string): Chainable // Category delete_category(name: string): Chainable + add_category(name: string): Chainable } } } diff --git a/pages/admin/settings/categories.vue b/pages/admin/settings/categories.vue index 6fde6b6b..38dcb918 100644 --- a/pages/admin/settings/categories.vue +++ b/pages/admin/settings/categories.vue @@ -4,7 +4,7 @@