-
Notifications
You must be signed in to change notification settings - Fork 17
E2E test for deleting tags #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josecelano
merged 3 commits into
torrust:develop
from
mario-nt:#229-E2E-tests-delete-a-tag
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
|
|
||
| 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."); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.