feat: add signup and 404 e2e test (#322)

This commit is contained in:
Zhaoxinxin 2023-11-22 18:23:45 +08:00 committed by GitHub
parent 480b46bd0c
commit 28144ff820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 240 additions and 0 deletions

46
cypress/e2e/404.cy.ts Normal file
View File

@ -0,0 +1,46 @@
import root from '../fixtures/api/role-root.json';
import user from '../fixtures/api/user.json';
describe('404', () => {
beforeEach(() => {
cy.signin();
cy.intercept(
{
method: 'GET',
url: '/api/v1/users/1',
},
(req) => {
req.reply({
statusCode: 200,
body: user,
});
},
);
cy.intercept(
{
method: 'GET',
url: '/api/v1/users/1/roles',
},
(req) => {
req.reply({
statusCode: 200,
body: root,
});
},
);
cy.viewport(1440, 1080);
});
it('show 404 page', () => {
cy.visit('/root');
cy.get('.MuiTypography-h4').should('have.text', 'Something gone wrong!');
});
it('click the `go back cluster` button', () => {
cy.visit('/root');
cy.get('.MuiTypography-h4').should('have.text', 'Something gone wrong!');
cy.get('.MuiButtonBase-root').click();
// Then I see that the current page is the clusters
cy.url().should('include', '/clusters');
});
});

View File

@ -203,6 +203,27 @@ describe('Signin', () => {
cy.get('#password').should('not.have.text', 'dragonfly');
});
it('should handle API error response', () => {
cy.intercept('POST', '/api/v1/users/signin', (req) => {
(req.body = { name: 'root', password: 'dragonfly' }),
req.reply({
forceNetworkError: true,
});
});
cy.get('#account').type('root');
cy.get('#password').type(`dragonfly`);
cy.get('form').submit();
// show error message
cy.get('.MuiAlert-message').should('be.visible').and('contain', 'Failed to fetch');
// close error message
cy.get('.MuiAlert-action > .MuiButtonBase-root').click();
cy.get('.MuiSnackbar-root > .MuiPaper-root').should('not.exist');
});
it('try to verify account and password', () => {
const nameNotLongEnough = _.times(2, () => _.sample('abcdefghijklmnopqrstuvwxyz')).join('');
const nameLengthExceeds = _.times(11, () => _.sample('abcdefghijklmnopqrstuvwxyz')).join('');

159
cypress/e2e/signup.cy.ts Normal file
View File

@ -0,0 +1,159 @@
import signup from '../fixtures/api/signup.json';
import _ from 'lodash';
describe('Signup', () => {
beforeEach(() => {
cy.intercept('POST', '/api/v1/users/signup', (req) => {
const { name, password, email } = req.body;
if (name === 'root-1' && password === 'dragonfly1' && email === 'root@console.com') {
req.reply({
statusCode: 200,
body: signup,
});
} else {
req.reply({
statusCode: 409,
body: {
message: 'Conflict',
},
});
}
});
cy.visit('/signup');
});
it('can create user account', () => {
cy.get('#account').type('root-1');
cy.get('#email').type('root@console.com');
cy.get('#password').type('dragonfly1');
cy.get('#confirmPassword').type(`dragonfly1{enter}`);
// then I see that the current page is the signin
cy.url().should('include', '/signin');
});
it('cannot create account with existing email', () => {
cy.get('#account').type('root-1');
cy.get('#email').type('lucy@example.com');
cy.get('#password').type('dragonfly1');
cy.get('#confirmPassword').type(`dragonfly1{enter}`);
// show error message
cy.get('.MuiAlert-message').should('be.visible').and('contain', 'Conflict');
// close error message
cy.get('.MuiAlert-action > .MuiButtonBase-root').click();
cy.get('.MuiSnackbar-root > .MuiPaper-root').should('not.exist');
});
it('cannot create account with existing account', () => {
cy.get('#account').type('root');
cy.get('#email').type('root@console.co');
cy.get('#password').type('dragonfly1');
cy.get('#confirmPassword').type(`dragonfly1{enter}`);
// show error message
cy.get('.MuiAlert-message').should('be.visible').and('contain', 'Conflict');
// close error message
cy.get('.MuiAlert-action > .MuiButtonBase-root').click();
cy.get('.MuiSnackbar-root > .MuiPaper-root').should('not.exist');
});
it('click the password hide butto and confirm password hide butto', () => {
cy.get('#password').type('dragonfly1');
cy.get('#confirmPassword').type(`dragonfly1{enter}`);
cy.get('.MuiInputBase-root > .MuiButtonBase-root > [data-testid="VisibilityOffIcon"] > path').click();
cy.get('#confirmPassword').should('have.value', 'dragonfly1');
cy.get('[data-testid="VisibilityOffIcon"]').click();
cy.get('#confirmPassword').should('have.value', 'dragonfly1');
});
it('cannot signup without required attributes', () => {
cy.get('.MuiButton-root').click();
cy.get('#account-helper-text').should('be.visible').and('contain', 'Fill in the characters, the length is 3-10.');
cy.get('#email-helper-text').should('be.visible').and('contain', 'Email is invalid or already taken.');
cy.get('#confirmPassword-helper-text').should('be.visible').and('contain', 'Please enter the same password.');
cy.get('#confirmPassword-helper-text').should('be.visible').and('contain', 'Please enter the same password.');
});
it('should handle API error response', () => {
cy.intercept('POST', '/api/v1/users/signup', (req) => {
(req.body = { name: 'root-1', password: 'dragonrly1', email: 'root@console.com' }),
req.reply({
forceNetworkError: true,
});
});
cy.get('#account').type('root-1');
cy.get('#email').type('root@console.com');
cy.get('#password').type('dragonfly1');
cy.get('#confirmPassword').type(`dragonfly1{enter}`);
// show error message
cy.get('.MuiAlert-message').should('be.visible').and('contain', 'Failed to fetch');
// close error message
cy.get('.MuiAlert-action > .MuiButtonBase-root').click();
cy.get('.MuiSnackbar-root > .MuiPaper-root').should('not.exist');
});
it('click the `Sign in` button', () => {
cy.get('.MuiTypography-inherit > .MuiTypography-root').click();
// Then I see that the current page is the signin
cy.url().should('include', '/signin');
cy.get('.MuiTypography-inherit > .MuiTypography-root').click();
// Then I see that the current page is the signup
cy.url().should('include', '/signup');
});
it('cannot signup with invalid attributes', () => {
const nameNotLongEnough = _.times(2, () => _.sample('abcdefghijklmnopqrstuvwxyz')).join('');
const nameLengthExceeds = _.times(11, () => _.sample('abcdefghijklmnopqrstuvwxyz')).join('');
const passsword = _.times(8, () => _.sample('abcdefghijklmnopqrstuvwxyz')).join('');
cy.get('#account').type(nameNotLongEnough);
cy.get('#account-helper-text').should('be.visible').and('contain', 'Fill in the characters, the length is 3-10.');
cy.get('#account').type(nameLengthExceeds);
cy.get('#account-helper-text').should('be.visible').and('contain', 'Fill in the characters, the length is 3-10.');
cy.get('#account').clear();
cy.get('#account').type('root');
// verification passed
cy.get('#account-helper-text').should('not.exist');
cy.get('#email').type('root');
cy.get('#email-helper-text').should('be.visible').and('contain', 'Email is invalid or already taken.');
cy.get('#email').clear();
cy.get('#email').type('root@console.com');
// verification passed
cy.get('#email-helper-text').should('not.exist');
cy.get('#password').type(passsword);
// missing number
cy.get('#password-helper-text')
.should('be.visible')
.and('contain', 'At least 8-16 characters, with at least 1 lowercase letter and 1 number.');
cy.get('#password').clear();
cy.get('#password').type('dragonfly1');
// verification passed
cy.get('#password-helper-text').should('not.exist');
cy.get('#confirmPassword').type(`dragonfly`);
// confirm password verification error when the two passwords are not the same
cy.get('#confirmPassword-helper-text').should('be.visible').and('contain', 'Please enter the same password.');
cy.get('#confirmPassword').clear();
cy.get('#confirmPassword').type('dragonfly1');
// verification passed
cy.get('#confirmPassword-helper-text').should('not.exist');
});
});

View File

@ -0,0 +1,14 @@
{
"id": 2,
"created_at": "2023-11-08T10:26:26Z",
"updated_at": "2023-11-08T10:26:26Z",
"is_del": 0,
"email": "root@console.com",
"name": "Jack",
"avatar": "https://example.com/avatar.png",
"phone": "1234567890",
"state": "enable",
"location": "ShangHai",
"bio": "I am jack",
"configs": null
}