Skip to content

Commit c969ad3

Browse files
author
MMelchor
committed
test: [#310] added tests for a guest user to see the list page when there are no torrents in the index
1 parent d0134fc commit c969ad3

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

cypress.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from "cypress";
22
import { grantAdminRole, deleteUser } from "./cypress/e2e/contexts/user/tasks";
3-
import { deleteTorrent } from "./cypress/e2e/contexts/torrent/tasks";
3+
import { deleteTorrent, deleteTorrentsInfoFromDatabase } from "./cypress/e2e/contexts/torrent/tasks";
44
import { deleteCategory, addCategory } from "./cypress/e2e/contexts/category/tasks";
55
import { DatabaseConfig } from "./cypress/e2e/common/database";
66

@@ -26,6 +26,9 @@ export default defineConfig({
2626
deleteTorrent: ({ infohash }) => {
2727
return deleteTorrent(infohash, databaseConfig(config));
2828
},
29+
deleteTorrentsInfoFromDatabase: () => {
30+
return deleteTorrentsInfoFromDatabase(databaseConfig(config));
31+
},
2932
// User context
3033
grantAdminRole: ({ username }) => {
3134
return grantAdminRole(username, databaseConfig(config));

cypress/e2e/contexts/torrent/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ Cypress.Commands.add("delete_torrent_from_database_and_fixture", (torrent_info,
4040
// Delete the torrent file in the fixtures folder
4141
cy.exec(`rm ${torrent_info.path}`);
4242
});
43+
44+
Cypress.Commands.add("clear_torrent_info_from_database", () => {
45+
cy.task("deleteTorrentsInfoFromDatabase");
46+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { baseURL } from "nuxt/dist/core/runtime/nitro/paths";
2+
3+
describe("A guest user", () => {
4+
before(() => {
5+
// Deletes all torrents and their related info from the database so the test can pass
6+
cy.clear_torrent_info_from_database();
7+
});
8+
9+
it("Should be able to see the list page when there are no torrents", () => {
10+
cy.visit("/torrents");
11+
cy.url().should("match", /\/torrents$/);
12+
cy.get("[data-cy=\"no-results-element\"]").invoke("text").should("match", /No results./i);
13+
});
14+
});

cypress/e2e/contexts/torrent/tasks.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,69 @@ function deleteTorrentQuery (infohash: string): DatabaseQuery {
2020
params: [infohash]
2121
};
2222
}
23+
24+
// Task to delete all torrents from the database before running any test
25+
export const deleteTorrentsInfoFromDatabase = async (db_config: DatabaseConfig): Promise<Boolean> => {
26+
try {
27+
await runDatabaseQuery(clearTorrentsTableQuery(), db_config);
28+
await runDatabaseQuery(clearAnnounceUrlsTableQuery(), db_config);
29+
await runDatabaseQuery(clearTorrentFilesTableQuery(), db_config);
30+
await runDatabaseQuery(clearTorrentInfoTableQuery(), db_config);
31+
await runDatabaseQuery(clearTorrentInfoHashesTableQuery(), db_config);
32+
await runDatabaseQuery(clearTagLinkTableQuery(), db_config);
33+
await runDatabaseQuery(clearTrackerStatsTableQuery(), db_config);
34+
return true;
35+
} catch (err) {
36+
return await Promise.reject(err);
37+
}
38+
};
39+
40+
// Database query specifications
41+
function clearTorrentsTableQuery (): DatabaseQuery {
42+
return {
43+
query: "DELETE FROM torrust_torrents",
44+
params: []
45+
};
46+
}
47+
48+
function clearAnnounceUrlsTableQuery (): DatabaseQuery {
49+
return {
50+
query: "DELETE FROM torrust_torrent_announce_urls",
51+
params: []
52+
};
53+
}
54+
55+
function clearTorrentFilesTableQuery (): DatabaseQuery {
56+
return {
57+
query: "DELETE FROM torrust_torrent_files",
58+
params: []
59+
};
60+
}
61+
62+
function clearTorrentInfoTableQuery (): DatabaseQuery {
63+
return {
64+
query: "DELETE FROM torrust_torrent_info",
65+
params: []
66+
};
67+
}
68+
69+
function clearTorrentInfoHashesTableQuery (): DatabaseQuery {
70+
return {
71+
query: "DELETE FROM torrust_torrent_info_hashes",
72+
params: []
73+
};
74+
}
75+
76+
function clearTagLinkTableQuery (): DatabaseQuery {
77+
return {
78+
query: "DELETE FROM torrust_torrent_tag_links",
79+
params: []
80+
};
81+
}
82+
83+
function clearTrackerStatsTableQuery (): DatabaseQuery {
84+
return {
85+
query: "DELETE FROM torrust_torrent_tracker_stats",
86+
params: []
87+
};
88+
}

cypress/support/commands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import "../e2e/contexts/user/commands";
22
import "../e2e/contexts/torrent/commands";
33
import "../e2e/contexts/category/commands";
44
import "../e2e/common/commands";
5-
import { TestTorrentInfo } from "cypress/e2e/contexts/torrent/test_torrent_info";
65
import { RegistrationForm } from "../e2e/contexts/user/registration";
6+
import { TestTorrentInfo } from "cypress/e2e/contexts/torrent/test_torrent_info";
77

88
declare global {
99
namespace Cypress {
@@ -27,6 +27,7 @@ declare global {
2727
// Torrent context
2828
upload_torrent(torrent_info: TestTorrentInfo): Chainable<void>
2929
delete_torrent_from_database_and_fixture(torrent_info: TestTorrentInfo, infohash: string): Chainable<void>
30+
clear_torrent_info_from_database(): Chainable<void>;
3031

3132
// Category context
3233
delete_category_from_database(name: string): Chainable<void>

pages/torrents.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<Pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :total-results="torrentsTotal" />
5252
</template>
5353
<template v-else>
54-
<span class="text-neutral-content">No results.</span>
54+
<span data-cy="no-results-element" class="text-neutral-content">No results.</span>
5555
</template>
5656
</div>
5757
</div>

0 commit comments

Comments
 (0)