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

duplication fix #138

Merged
merged 1 commit into from
Apr 27, 2024
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
44 changes: 21 additions & 23 deletions webapp/src/components/auth/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ const mockAxios = new MockAdapter(axios);


describe('AddUser component', () => {
let usernameInput = 0;
let emailInput = 0;
let passwordInput = 0;
let cpasswordInput = 0;
let addUserButton = 0;

beforeEach(() => {
mockAxios.reset();
});

it('renders correctly', () => {
render(
<BrowserRouter>
<AddUser />
</BrowserRouter>);
usernameInput = screen.getByLabelText(/Username/i);
emailInput = screen.getByLabelText(/Email/i);
passwordInput = screen.getByLabelText("Password");
cpasswordInput = screen.getByLabelText(/Confirm Password/i);
addUserButton = screen.getByRole('button', { name: /Register/i });
});

it('renders correctly', () => {


expect(screen.getByLabelText('Username')).toBeInTheDocument();
expect(screen.getByLabelText('Email')).toBeInTheDocument();
Expand All @@ -30,16 +42,10 @@ describe('AddUser component', () => {
});

it('should add user successfully', async () => {
render(<BrowserRouter>
<AddUser />
</BrowserRouter>);



const usernameInput = screen.getByLabelText(/Username/i);
const emailInput = screen.getByLabelText(/Email/i);
const passwordInput = screen.getByLabelText("Password");
const cpasswordInput = screen.getByLabelText(/Confirm Password/i);
const addUserButton = screen.getByRole('button', { name: /Register/i });


// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
Expand All @@ -60,13 +66,9 @@ describe('AddUser component', () => {
});

it('should handle wrong passwords when adding user', async () => {
render(<BrowserRouter>
<AddUser />
</BrowserRouter>);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText("Password");
const addUserButton = screen.getByRole('button', { name: /Register/i });



// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });
Expand All @@ -86,13 +88,9 @@ describe('AddUser component', () => {
});

it('should handle error when adding user', async () => {
render(<BrowserRouter>
<AddUser />
</BrowserRouter>);


const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText("Password");
const addUserButton = screen.getByRole('button', { name: /Register/i });


// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });
Expand Down
61 changes: 24 additions & 37 deletions webapp/src/components/auth/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,58 @@ import createStore from 'react-auth-kit/createStore';
const mockAxios = new MockAdapter(axios);

describe('Login component', () => {
let usernameInput = 0;
let passwordInput = 0;
let loginButton = 0;
const store = createStore({
authName: '_auth',
authType: 'cookie',
cookieDomain: window.location.hostname,
cookieSecure: window.location.protocol === 'https:',
});
beforeEach(() => {
mockAxios.reset();
});

it('should log in successfully', async () => {
const store = createStore({
authName: '_auth',
authType: 'cookie',
cookieDomain: window.location.hostname,
cookieSecure: window.location.protocol === 'https:',
});


render(
<AuthProvider
store={store}
>
<BrowserRouter>
<Login/>
<Login />
</BrowserRouter>
</AuthProvider>);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Log In/i });

usernameInput = screen.getByLabelText(/Username/i);
passwordInput = screen.getByLabelText(/Password/i);
loginButton = screen.getByRole('button', { name: /Log In/i });
});

it('should log in successfully', async () => {

const mock = jest.fn();
jest.mock('react-router-dom', () => ({
useNavigate: () => mock,
}));
// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/login').reply(200, { username:"testUser",email:"[email protected]",createdAt: '2024-01-01T12:34:56Z',token: 'testToken'});
mockAxios.onPost('http://localhost:8000/login').reply(200, { username: "testUser", email: "[email protected]", createdAt: '2024-01-01T12:34:56Z', token: 'testToken' });

// Simulate user input
await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
fireEvent.click(loginButton);
});


const linkElement = screen.getByText(/Error: Error: There was a problem.../i);
expect(linkElement).toBeInTheDocument();


});

it('should handle error when logging in', async () => {
const store = createStore({
authName: '_auth',
authType: 'cookie',
cookieDomain: window.location.hostname,
cookieSecure: window.location.protocol === 'https:',
});

render(
<AuthProvider
store={store}
>
<BrowserRouter>
<Login/>
</BrowserRouter>
</AuthProvider>);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Log In/i });


// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Unauthorized' });
Expand All @@ -92,5 +79,5 @@ describe('Login component', () => {

});


});