Skip to content
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

E2E test: add new category #225

Merged
merged 2 commits into from
Aug 11, 2023
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
4 changes: 4 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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 { DatabaseConfig } from "./cypress/e2e/common/database";

function databaseConfig (config: Cypress.PluginConfigOptions): DatabaseConfig {
Expand All @@ -22,6 +23,9 @@ export default defineConfig({
},
deleteUser: ({ username }) => {
return deleteUser(username, databaseConfig(config));
},
deleteCategory: ({ name }) => {
return deleteCategory(name, databaseConfig(config));
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions cypress/e2e/contexts/category/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Custom commands for category context

Cypress.Commands.add("delete_category", (name) => {
cy.task("deleteCategory", { name });
});
64 changes: 64 additions & 0 deletions cypress/e2e/contexts/category/specs/add.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { RegistrationForm, random_user_registration_data } from "../../user/registration";

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 add a new category", () => {
// Make sure the category does not exist
cy.delete_category("new category");

// 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();

// Fill new category name
cy.get("input[data-cy=\"add-category-input\"]").type("new category");

// Add category
cy.get("button[data-cy=\"add-category-button\"]").click();

// The new category should appear in the list
cy.contains("new category (0)");

cy.delete_category("new category");
});
});

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 add a new category", () => {
cy.visit("/admin/settings/categories");

cy.contains("Please login to manage admin settings.");
});
});

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.");
});
});
22 changes: 22 additions & 0 deletions cypress/e2e/contexts/category/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Custom tasks for category context

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

// Task to delete a category
export const deleteCategory = async (name: string, db_config: DatabaseConfig): Promise<string> => {
try {
const result = await runDatabaseQuery(deleteCategoryQuery(name), db_config);
return name;
} catch (err) {
return await Promise.reject(err);
}
};

// Database query specifications

function deleteCategoryQuery (name: string): DatabaseQuery {
return {
query: "DELETE FROM torrust_categories WHERE name = ?",
params: [name]
};
}
6 changes: 1 addition & 5 deletions cypress/e2e/contexts/torrent/specs/download.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ describe("A registered user", () => {

before(() => {
registration_form = random_user_registration_data();

cy.visit("/");
cy.visit("/signup");
cy.register(registration_form);
cy.login(registration_form.username, registration_form.password);
cy.register_and_login(registration_form);
});

after(() => {
Expand Down
6 changes: 1 addition & 5 deletions cypress/e2e/contexts/torrent/specs/upload.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ describe("A registered user", () => {

before(() => {
registration_form = random_user_registration_data();

cy.visit("/");
cy.visit("/signup");
cy.register(registration_form);
cy.login(registration_form.username, registration_form.password);
cy.register_and_login(registration_form);
});

after(() => {
Expand Down
16 changes: 16 additions & 0 deletions cypress/e2e/contexts/user/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,19 @@ Cypress.Commands.add("login", (username: string, password: string) => {
Cypress.Commands.add("logout", () => {
cy.get("a[data-cy=\"logout-link\"]").click();
});

// Others

Cypress.Commands.add("register_and_login", (registration_form) => {
cy.visit("/");
cy.visit("/signup");
cy.register(registration_form);
cy.login(registration_form.username, registration_form.password);
});

Cypress.Commands.add("register_as_admin_and_login", (registration_form) => {
cy.visit("/");
cy.visit("/signup");
cy.register_as_admin(registration_form);
cy.login(registration_form.username, registration_form.password);
});
6 changes: 6 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "../e2e/contexts/user/commands";
import "../e2e/contexts/torrent/commands";
import "../e2e/contexts/category/commands";
import { RegistrationForm } from "../e2e/contexts/user/registration";
import { TestTorrentInfo } from "cypress/e2e/contexts/torrent/test_torrent_info";

Expand All @@ -13,9 +14,14 @@ declare global {
// User: Authentication
login(username: string, password: string): Chainable<void>
logout(): Chainable<void>
// User: others
register_and_login(registration_form: RegistrationForm): Chainable<void>
register_as_admin_and_login(registration_form: RegistrationForm): Chainable<void>
// Torrent
upload_torrent(torrent_info: TestTorrentInfo): Chainable<void>
delete_torrent(torrent_info: TestTorrentInfo, infohash: string): Chainable<void>
// Category
delete_category(name: string): Chainable<void>
}
}
}
5 changes: 5 additions & 0 deletions pages/admin/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<template v-if="settings">
<NuxtPage :settings="settings" class="w-full" />
</template>
<template v-else>
<div class="flex flex-col gap-2">
Please login to manage admin settings.
</div>
</template>

</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions pages/admin/settings/categories.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</template>
</div>
<div class="flex gap-2">
<input v-model="newCategory" class="w-full input input-bordered" type="text">
<button class="btn btn-primary" :class="{ 'loading': addingCategory }" :disabled="addingCategory || !newCategory" @click="addCategory">
<input v-model="newCategory" data-cy="add-category-input" class="w-full input input-bordered" type="text">
<button data-cy="add-category-button" class="btn btn-primary" :class="{ 'loading': addingCategory }" :disabled="addingCategory || !newCategory" @click="addCategory">
Add category
</button>
</div>
Expand Down