add api-testing to cypress tests

This commit is contained in:
Francis Cao 2025-03-12 10:32:54 -07:00
parent abde966647
commit a407ff4693
13 changed files with 598 additions and 46 deletions

209
cypress/e2e/api-team.cy.ts Normal file
View file

@ -0,0 +1,209 @@
describe('Team API tests', () => {
Cypress.session.clearAllSavedSessions();
let teamId;
let userId;
before(() => {
cy.login(Cypress.env('umami_user'), Cypress.env('umami_password'));
cy.fixture('users').then(data => {
const userCreate = data.userCreate;
cy.request({
method: 'POST',
url: '/api/users',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: userCreate,
}).then(response => {
userId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('username', 'cypress1');
expect(response.body).to.have.property('role', 'user');
});
});
});
it('Creates a team.', () => {
cy.fixture('teams').then(data => {
const teamCreate = data.teamCreate;
cy.request({
method: 'POST',
url: '/api/teams',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: teamCreate,
}).then(response => {
teamId = response.body[0].id;
expect(response.status).to.eq(200);
expect(response.body[0]).to.have.property('name', 'cypress');
expect(response.body[1]).to.have.property('role', 'team-owner');
});
});
});
it('Gets a teams by ID.', () => {
cy.request({
method: 'GET',
url: `/api/teams/${teamId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('id', teamId);
});
});
it('Updates a team.', () => {
cy.fixture('teams').then(data => {
const teamUpdate = data.teamUpdate;
cy.request({
method: 'POST',
url: `/api/teams/${teamId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: teamUpdate,
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('id', teamId);
expect(response.body).to.have.property('name', 'cypressUpdate');
});
});
});
it('Get all users that belong to a team.', () => {
cy.request({
method: 'GET',
url: `/api/teams/${teamId}/users`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body.data[0]).to.have.property('id');
expect(response.body.data[0]).to.have.property('teamId');
expect(response.body.data[0]).to.have.property('userId');
expect(response.body.data[0]).to.have.property('user');
});
});
it('Get a user belonging to a team.', () => {
cy.request({
method: 'GET',
url: `/api/teams/${teamId}/users/${Cypress.env('umami_user_id')}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('teamId');
expect(response.body).to.have.property('userId');
expect(response.body).to.have.property('role');
});
});
it('Get all websites belonging to a team.', () => {
cy.request({
method: 'GET',
url: `/api/teams/${teamId}/websites`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('data');
});
});
it('Add a user to a team.', () => {
cy.request({
method: 'POST',
url: `/api/teams/${teamId}/users`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: {
userId,
role: 'team-member',
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('userId', userId);
expect(response.body).to.have.property('role', 'team-member');
});
});
it(`Update a user's role on a team.`, () => {
cy.request({
method: 'POST',
url: `/api/teams/${teamId}/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: {
role: 'team-view-only',
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('userId', userId);
expect(response.body).to.have.property('role', 'team-view-only');
});
});
it(`Remove a user from a team.`, () => {
cy.request({
method: 'DELETE',
url: `/api/teams/${teamId}/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
});
});
it('Deletes a team.', () => {
cy.request({
method: 'DELETE',
url: `/api/teams/${teamId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('ok', true);
});
});
// it('Gets all teams that belong to a user.', () => {
// cy.request({
// method: 'GET',
// url: `/api/users/${userId}/teams`,
// headers: {
// 'Content-Type': 'application/json',
// Authorization: Cypress.env('authorization'),
// },
// }).then(response => {
// expect(response.status).to.eq(200);
// expect(response.body).to.have.property('data');
// });
// });
after(() => {
cy.deleteUser(userId);
});
});

125
cypress/e2e/api-user.cy.ts Normal file
View file

@ -0,0 +1,125 @@
describe('User API tests', () => {
Cypress.session.clearAllSavedSessions();
before(() => {
cy.login(Cypress.env('umami_user'), Cypress.env('umami_password'));
});
let userId;
it('Creates a user.', () => {
cy.fixture('users').then(data => {
const userCreate = data.userCreate;
cy.request({
method: 'POST',
url: '/api/users',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: userCreate,
}).then(response => {
userId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('username', 'cypress1');
expect(response.body).to.have.property('role', 'user');
});
});
});
it('Returns all users. Admin access is required.', () => {
cy.request({
method: 'GET',
url: '/api/admin/users',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body.data[0]).to.have.property('id');
expect(response.body.data[0]).to.have.property('username');
expect(response.body.data[0]).to.have.property('password');
expect(response.body.data[0]).to.have.property('role');
});
});
it('Updates a user.', () => {
cy.fixture('users').then(data => {
const userUpdate = data.userUpdate;
cy.request({
method: 'POST',
url: `/api/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: userUpdate,
}).then(response => {
userId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('id', userId);
expect(response.body).to.have.property('username', 'cypress1');
expect(response.body).to.have.property('role', 'view-only');
});
});
});
it('Gets a user by ID.', () => {
cy.request({
method: 'GET',
url: `/api/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('id', userId);
expect(response.body).to.have.property('username', 'cypress1');
expect(response.body).to.have.property('role', 'view-only');
});
});
it('Deletes a user.', () => {
cy.request({
method: 'DELETE',
url: `/api/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('ok', true);
});
});
it('Gets all websites that belong to a user.', () => {
cy.request({
method: 'GET',
url: `/api/users/${userId}/websites`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('data');
});
});
it('Gets all teams that belong to a user.', () => {
cy.request({
method: 'GET',
url: `/api/users/${userId}/teams`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('data');
});
});
});

View file

@ -0,0 +1,150 @@
describe('Website API tests', () => {
Cypress.session.clearAllSavedSessions();
let websiteId;
let teamId;
before(() => {
cy.login(Cypress.env('umami_user'), Cypress.env('umami_password'));
cy.fixture('teams').then(data => {
const teamCreate = data.teamCreate;
cy.request({
method: 'POST',
url: '/api/teams',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: teamCreate,
}).then(response => {
teamId = response.body[0].id;
expect(response.status).to.eq(200);
expect(response.body[0]).to.have.property('name', 'cypress');
expect(response.body[1]).to.have.property('role', 'team-owner');
});
});
});
it('Creates a website for user.', () => {
cy.fixture('websites').then(data => {
const websiteCreate = data.websiteCreate;
cy.request({
method: 'POST',
url: '/api/websites',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: websiteCreate,
}).then(response => {
websiteId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('name', 'Cypress Website');
expect(response.body).to.have.property('domain', 'cypress.com');
});
});
});
it('Creates a website for team.', () => {
cy.request({
method: 'POST',
url: '/api/websites',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: {
name: 'Team Website',
domain: 'teamwebsite.com',
teamId: teamId,
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('name', 'Team Website');
expect(response.body).to.have.property('domain', 'teamwebsite.com');
});
});
it('Returns all tracked websites.', () => {
cy.request({
method: 'GET',
url: '/api/websites',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body.data[0]).to.have.property('id');
expect(response.body.data[0]).to.have.property('name');
expect(response.body.data[0]).to.have.property('domain');
});
});
it('Gets a website by ID.', () => {
cy.request({
method: 'GET',
url: `/api/websites/${websiteId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('name', 'Cypress Website');
expect(response.body).to.have.property('domain', 'cypress.com');
});
});
it('Updates a website.', () => {
cy.fixture('websites').then(data => {
const websiteUpdate = data.websiteUpdate;
cy.request({
method: 'POST',
url: `/api/websites/${websiteId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: websiteUpdate,
}).then(response => {
websiteId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('name', 'Cypress Website Updated');
expect(response.body).to.have.property('domain', 'cypressupdated.com');
});
});
});
it('Resets a website by removing all data related to the website.', () => {
cy.request({
method: 'POST',
url: `/api/websites/${websiteId}/reset`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('ok', true);
});
});
it('Deletes a website.', () => {
cy.request({
method: 'DELETE',
url: `/api/websites/${websiteId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('ok', true);
});
});
after(() => {
cy.deleteTeam(teamId);
});
});

View file

@ -1,29 +0,0 @@
describe('Website tests', () => {
Cypress.session.clearAllSavedSessions();
beforeEach(() => {
cy.login(Cypress.env('umami_user'), Cypress.env('umami_password'));
});
//let userId;
it('creates a user.', () => {
cy.fixture('users').then(data => {
const userPost = data.userPost;
cy.request({
method: 'POST',
url: '/api/users',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: userPost,
}).then(response => {
//userId = response.body.id;
expect(response.status).to.eq(200);
expect(response.body).to.have.property('username', 'cypress1');
expect(response.body).to.have.property('role', 'User');
});
});
});
});

View file

@ -1,4 +1,4 @@
describe('Website tests', () => {
describe('User tests', () => {
Cypress.session.clearAllSavedSessions();
beforeEach(() => {
@ -51,7 +51,7 @@ describe('Website tests', () => {
cy.url().should('eq', Cypress.config().baseUrl + '/dashboard');
});
it('Delete a website', () => {
it('Delete a user', () => {
// delete user
cy.get('table tbody tr')
.contains('td', /Test-user/i)

View file

@ -0,0 +1,8 @@
{
"teamCreate": {
"name": "cypress"
},
"teamUpdate": {
"name": "cypressUpdate"
}
}

View file

@ -1,17 +1,11 @@
{
"userGet": {
"name": "cypress",
"email": "password",
"role": "User"
},
"userPost": {
"userCreate": {
"username": "cypress1",
"password": "password",
"role": "User"
"role": "user"
},
"userDelete": {
"name": "Charlie",
"email": "charlie@example.com",
"age": 35
"userUpdate": {
"username": "cypress1",
"role": "view-only"
}
}

View file

@ -0,0 +1,10 @@
{
"websiteCreate": {
"name": "Cypress Website",
"domain": "cypress.com"
},
"websiteUpdate": {
"name": "Cypress Website Updated",
"domain": "cypressupdated.com"
}
}

View file

@ -61,3 +61,63 @@ Cypress.Commands.add('deleteWebsite', (websiteId: string) => {
expect(response.status).to.eq(200);
});
});
Cypress.Commands.add('addUser', (username: string, password: string, role: string) => {
cy.request({
method: 'POST',
url: '/api/users',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: {
username: username,
password: password,
role: role,
},
}).then(response => {
expect(response.status).to.eq(200);
});
});
Cypress.Commands.add('deleteUser', (userId: string) => {
cy.request({
method: 'DELETE',
url: `/api/users/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
});
});
Cypress.Commands.add('addTeam', (name: string) => {
cy.request({
method: 'POST',
url: '/api/teams',
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
body: {
name: name,
},
}).then(response => {
expect(response.status).to.eq(200);
});
});
Cypress.Commands.add('deleteTeam', (teamId: string) => {
cy.request({
method: 'DELETE',
url: `/api/teams/${teamId}`,
headers: {
'Content-Type': 'application/json',
Authorization: Cypress.env('authorization'),
},
}).then(response => {
expect(response.status).to.eq(200);
});
});

View file

@ -24,9 +24,33 @@ declare namespace Cypress {
*/
addWebsite(name: string, domain: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to create a website
* Custom command to delete a website
* @example cy.deleteWebsite('02d89813-7a72-41e1-87f0-8d668f85008b')
*/
deleteWebsite(websiteId: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to create a website
* @example cy.deleteWebsite('02d89813-7a72-41e1-87f0-8d668f85008b')
*/
/**
* Custom command to create a user
* @example cy.addUser('cypress', 'password', 'User')
*/
addUser(username: string, password: string, role: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to delete a user
* @example cy.deleteUser('02d89813-7a72-41e1-87f0-8d668f85008b')
*/
deleteUser(userId: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to create a team
* @example cy.addTeam('cypressTeam')
*/
addTeam(name: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to create a website
* @example cy.deleteTeam('02d89813-7a72-41e1-87f0-8d668f85008b')
*/
deleteTeam(teamId: string): Chainable<JQuery<HTMLElement>>;
}
}