From b1e9ab6b3a7f89295d22781b73542c21fe176b74 Mon Sep 17 00:00:00 2001 From: MMelchor Date: Thu, 19 Oct 2023 10:27:45 +0200 Subject: [PATCH 1/3] test: [#310] added tests for a guest user to see the list page when there are no torrents in the index --- cypress.config.ts | 5 +- cypress/e2e/contexts/torrent/commands.ts | 4 ++ .../specs/list/no_torrents_to_display.cy.ts | 14 ++++ cypress/e2e/contexts/torrent/tasks.ts | 66 +++++++++++++++++++ cypress/support/commands.ts | 1 + pages/torrents.vue | 2 +- 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts diff --git a/cypress.config.ts b/cypress.config.ts index c694f08c..8471c0a3 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -1,6 +1,6 @@ import { defineConfig } from "cypress"; import { grantAdminRole, deleteUser } from "./cypress/e2e/contexts/user/tasks"; -import { deleteTorrent } from "./cypress/e2e/contexts/torrent/tasks"; +import { deleteTorrent, deleteTorrentsInfoFromDatabase } from "./cypress/e2e/contexts/torrent/tasks"; import { deleteCategory, addCategory } from "./cypress/e2e/contexts/category/tasks"; import { DatabaseConfig } from "./cypress/e2e/common/database"; @@ -26,6 +26,9 @@ export default defineConfig({ deleteTorrent: ({ infohash }) => { return deleteTorrent(infohash, databaseConfig(config)); }, + deleteTorrentsInfoFromDatabase: () => { + return deleteTorrentsInfoFromDatabase(databaseConfig(config)); + }, // User context grantAdminRole: ({ username }) => { return grantAdminRole(username, databaseConfig(config)); diff --git a/cypress/e2e/contexts/torrent/commands.ts b/cypress/e2e/contexts/torrent/commands.ts index 47d76bca..c1362928 100644 --- a/cypress/e2e/contexts/torrent/commands.ts +++ b/cypress/e2e/contexts/torrent/commands.ts @@ -40,3 +40,7 @@ Cypress.Commands.add("delete_torrent_from_database_and_fixture", (torrent_info, // Delete the torrent file in the fixtures folder cy.exec(`rm ${torrent_info.path}`); }); + +Cypress.Commands.add("clear_torrent_info_from_database", () => { + cy.task("deleteTorrentsInfoFromDatabase"); +}); diff --git a/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts new file mode 100644 index 00000000..68eb060c --- /dev/null +++ b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts @@ -0,0 +1,14 @@ +import { baseURL } from "nuxt/dist/core/runtime/nitro/paths"; + +describe("A guest user", () => { + before(() => { + // Deletes all torrents and their related info from the database so the test can pass + cy.clear_torrent_info_from_database(); + }); + + it("Should be able to see the list page when there are no torrents", () => { + cy.visit("/torrents"); + cy.url().should("match", /\/torrents$/); + cy.get("[data-cy=\"no-results-element\"]").invoke("text").should("match", /No results./i); + }); +}); diff --git a/cypress/e2e/contexts/torrent/tasks.ts b/cypress/e2e/contexts/torrent/tasks.ts index 82b4097c..cd70d3ae 100644 --- a/cypress/e2e/contexts/torrent/tasks.ts +++ b/cypress/e2e/contexts/torrent/tasks.ts @@ -20,3 +20,69 @@ function deleteTorrentQuery (infohash: string): DatabaseQuery { params: [infohash] }; } + +// Task to delete all torrents from the database before running any test +export const deleteTorrentsInfoFromDatabase = async (db_config: DatabaseConfig): Promise => { + try { + await runDatabaseQuery(clearTorrentsTableQuery(), db_config); + await runDatabaseQuery(clearAnnounceUrlsTableQuery(), db_config); + await runDatabaseQuery(clearTorrentFilesTableQuery(), db_config); + await runDatabaseQuery(clearTorrentInfoTableQuery(), db_config); + await runDatabaseQuery(clearTorrentInfoHashesTableQuery(), db_config); + await runDatabaseQuery(clearTagLinkTableQuery(), db_config); + await runDatabaseQuery(clearTrackerStatsTableQuery(), db_config); + return true; + } catch (err) { + return await Promise.reject(err); + } +}; + +// Database query specifications +function clearTorrentsTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrents", + params: [] + }; +} + +function clearAnnounceUrlsTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_announce_urls", + params: [] + }; +} + +function clearTorrentFilesTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_files", + params: [] + }; +} + +function clearTorrentInfoTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_info", + params: [] + }; +} + +function clearTorrentInfoHashesTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_info_hashes", + params: [] + }; +} + +function clearTagLinkTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_tag_links", + params: [] + }; +} + +function clearTrackerStatsTableQuery (): DatabaseQuery { + return { + query: "DELETE FROM torrust_torrent_tracker_stats", + params: [] + }; +} diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index ff9f76a7..eb78bd7d 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -27,6 +27,7 @@ declare global { // Torrent context upload_torrent(torrent_info: TestTorrentInfo): Chainable delete_torrent_from_database_and_fixture(torrent_info: TestTorrentInfo, infohash: string): Chainable + clear_torrent_info_from_database(): Chainable; // Category context delete_category_from_database(name: string): Chainable diff --git a/pages/torrents.vue b/pages/torrents.vue index 2a89341d..78cd3ca0 100644 --- a/pages/torrents.vue +++ b/pages/torrents.vue @@ -51,7 +51,7 @@ From 8e9b04c550b26b92dbed96a1c3d454284857d1ab Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 16 Nov 2023 14:19:27 +0100 Subject: [PATCH 2/3] refactor: [#310] fix minor typo in function name --- cypress/e2e/contexts/torrent/commands.ts | 2 +- .../contexts/torrent/specs/list/no_torrents_to_display.cy.ts | 2 +- cypress/support/commands.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cypress/e2e/contexts/torrent/commands.ts b/cypress/e2e/contexts/torrent/commands.ts index c1362928..39dce201 100644 --- a/cypress/e2e/contexts/torrent/commands.ts +++ b/cypress/e2e/contexts/torrent/commands.ts @@ -41,6 +41,6 @@ Cypress.Commands.add("delete_torrent_from_database_and_fixture", (torrent_info, cy.exec(`rm ${torrent_info.path}`); }); -Cypress.Commands.add("clear_torrent_info_from_database", () => { +Cypress.Commands.add("clear_torrents_info_from_database", () => { cy.task("deleteTorrentsInfoFromDatabase"); }); diff --git a/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts index 68eb060c..dd5ebf0f 100644 --- a/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts +++ b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts @@ -3,7 +3,7 @@ import { baseURL } from "nuxt/dist/core/runtime/nitro/paths"; describe("A guest user", () => { before(() => { // Deletes all torrents and their related info from the database so the test can pass - cy.clear_torrent_info_from_database(); + cy.clear_torrents_info_from_database(); }); it("Should be able to see the list page when there are no torrents", () => { diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index eb78bd7d..70998cb4 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -27,7 +27,7 @@ declare global { // Torrent context upload_torrent(torrent_info: TestTorrentInfo): Chainable delete_torrent_from_database_and_fixture(torrent_info: TestTorrentInfo, infohash: string): Chainable - clear_torrent_info_from_database(): Chainable; + clear_torrents_info_from_database(): Chainable; // Category context delete_category_from_database(name: string): Chainable From 6d6806de33440b474dcd1b7042d59dc89f044e65 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 16 Nov 2023 14:37:35 +0100 Subject: [PATCH 3/3] refactor: [#310] change describe to Users --- .../contexts/torrent/specs/list/no_torrents_to_display.cy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts index dd5ebf0f..6b6c8ec1 100644 --- a/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts +++ b/cypress/e2e/contexts/torrent/specs/list/no_torrents_to_display.cy.ts @@ -1,6 +1,6 @@ import { baseURL } from "nuxt/dist/core/runtime/nitro/paths"; -describe("A guest user", () => { +describe("Users", () => { before(() => { // Deletes all torrents and their related info from the database so the test can pass cy.clear_torrents_info_from_database();