Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from "cypress";
import { grantAdminRole, deleteUser } from "./cypress/e2e/contexts/user/tasks";
import { deleteTorrent, deleteTorrentsInfoFromDatabase } from "./cypress/e2e/contexts/torrent/tasks";
import { deleteCategory, addCategory } from "./cypress/e2e/contexts/category/tasks";
import { deleteTags, addTag } from "./cypress/e2e/contexts/tag/tasks";
import { DatabaseConfig } from "./cypress/e2e/common/database";

function databaseConfig (config: Cypress.PluginConfigOptions): DatabaseConfig {
Expand All @@ -22,6 +23,13 @@ export default defineConfig({
addCategory: ({ name }) => {
return addCategory(name, databaseConfig(config));
},
// Tag context
deleteTags: () => {
return deleteTags(databaseConfig(config));
},
addTag: ({ name }) => {
return addTag(name, databaseConfig(config));
},
// Torrent context
deleteTorrent: ({ infohash }) => {
return deleteTorrent(infohash, databaseConfig(config));
Expand Down
9 changes: 9 additions & 0 deletions cypress/e2e/contexts/tag/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Custom commands for tag context

Cypress.Commands.add("delete_tags_from_database", () => {
cy.task("deleteTags");
});

Cypress.Commands.add("add_tag_to_database", (name) => {
cy.task("addTag", { name });
});
8 changes: 8 additions & 0 deletions cypress/e2e/contexts/tag/random_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Logic for generating random data for tests

export function randomTagName (): string {
return `tag-${randomTagId()}`;
}
function randomTagId (): number {
return Math.floor(Math.random() * 1000000);
}
62 changes: 62 additions & 0 deletions cypress/e2e/contexts/tag/specs/delete.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { RegistrationForm, random_user_registration_data } from "../../user/registration";
import { randomTagName } from "../random_data";

describe("The admin user", () => {
const registration_form = random_user_registration_data();
const tag_name = randomTagName();
before(() => {
cy.delete_tags_from_database();
cy.register_as_admin_and_login(registration_form);
});

after(() => {
cy.delete_user_from_database(registration_form.username);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mario-nt, If the test fails, we could not have deleted the tag. Maybe we should try to delete it if it exists. If we want to keep the database clean after the test execution.

});

it("should be able to delete a tag", () => {
cy.add_tag_to_database(tag_name);

cy.go_to_settings();

// Click tags tab
cy.contains("a", "tags").click();

// Delete the tag
cy.get(`button[data-cy="delete-tag-${tag_name}"]`).click();

// Confirm alert should pop up
cy.on("window:confirm", (str) => {
expect(str).to.equal(`Are you sure you want to delete ${tag_name}?`);
});

// Confirm delete
cy.on("window:confirm", () => true);

cy.get(`[data-cy="delete-tag-${tag_name}"]`).should("not.exist");
});
});

describe("A non admin authenticated user", () => {
const registration_form = random_user_registration_data();

before(() => {
cy.delete_tags_from_database();
cy.register_and_login(registration_form);
});

after(() => {
cy.delete_user_from_database(registration_form.username);
});

it("should not be able to delete tags", () => {
cy.visit("/admin/settings/tags");
cy.contains("Please login to manage admin settings.");
});
});

describe("A guest user", () => {
it("should not be able to delete a tag", () => {
cy.visit("/admin/settings/tags");
cy.contains("Please login to manage admin settings.");
});
});
39 changes: 39 additions & 0 deletions cypress/e2e/contexts/tag/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Custom tasks for tag context

import { DatabaseConfig, DatabaseQuery, runDatabaseQuery } from "../../common/database";

// Task to delete a tag
export const deleteTags = async (db_config: DatabaseConfig): Promise<any> => {
try {
const result = await runDatabaseQuery(deleteTagsQuery(), db_config);
return {};
} catch (err) {
return await Promise.reject(err);
}
};

// Task to add a new tag
export const addTag = async (name: string, db_config: DatabaseConfig): Promise<string> => {
try {
const result = await runDatabaseQuery(addTagQuery(name), db_config);
return name;
} catch (err) {
return await Promise.reject(err);
}
};

// Database query specifications

function deleteTagsQuery (): DatabaseQuery {
return {
query: "DELETE FROM torrust_torrent_tags",
params: []
};
}

function addTagQuery (name: string): DatabaseQuery {
return {
query: "INSERT INTO torrust_torrent_tags (name) VALUES (?)",
params: [name]
};
}
5 changes: 5 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "../e2e/contexts/user/commands";
import "../e2e/contexts/torrent/commands";
import "../e2e/contexts/category/commands";
import "../e2e/contexts/tag/commands";
import "../e2e/common/commands";
import type { TestTorrentInfo } from "../e2e/contexts/torrent/test_torrent_info";
import type { RegistrationForm } from "../e2e/contexts/user/registration";
Expand Down Expand Up @@ -32,6 +33,10 @@ declare global {
// Category context
delete_category_from_database(name: string): Chainable<void>
add_category_to_database(name: string): Chainable<void>

// Tag context
delete_tags_from_database(): Chainable<void>
add_tag_to_database(name: string): Chainable<void>
}
}
}
10 changes: 9 additions & 1 deletion pages/admin/settings/tags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<template v-for="tag in tags">
<div class="flex justify-between p-2 rounded bg-base-100">
<span class="text-base-content">{{ tag.name }}</span>
<button class="text-error-content hover:text-error" @click="deleteTag(tag)">
<button :data-cy="getDeleteButtonDataCy(tag.name)" class="text-error-content hover:text-error" @click="deleteTag(tag)">
Delete
</button>
</div>
Expand All @@ -31,6 +31,14 @@ const rest = useRestApi().value;
const newTag = ref("");
const addingTag = ref(false);

onBeforeMount(() => {
getTags();
});

function getDeleteButtonDataCy (name: string) {
return "delete-tag-" + name.toLowerCase().replace(/ /g, "-");
}

function addTag () {
if (newTag.value) {
addingTag.value = true;
Expand Down