mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
dd1565936b
* moves login page styles to reusable component * Redirects successful login to homepage after 3s * Adds logout form * Adds logout page * Adds logout link to homepage * Adds gravatarURL to logged in user * Configure API Client to hit /me endpoint * Fetch user when the app loads * Configured API Client to make logout requests * Handle logout flow in redux * Logout form styles * Logout user when the logout form is submitted
141 lines
3.8 KiB
JavaScript
141 lines
3.8 KiB
JavaScript
import expect from 'expect';
|
|
import Kolide from './index';
|
|
import mocks from '../test/mocks';
|
|
|
|
const {
|
|
invalidForgotPasswordRequest,
|
|
invalidResetPasswordRequest,
|
|
validForgotPasswordRequest,
|
|
validLoginRequest,
|
|
validLogoutRequest,
|
|
validMeRequest,
|
|
validResetPasswordRequest,
|
|
validUser,
|
|
} = mocks;
|
|
|
|
describe('Kolide - API client', () => {
|
|
describe('defaults', () => {
|
|
it('sets the base URL', () => {
|
|
expect(Kolide.baseURL).toEqual('http://localhost:8080/api');
|
|
});
|
|
});
|
|
|
|
describe('#me', () => {
|
|
it('calls the appropriate endpoint with the correct parameters', (done) => {
|
|
const bearerToken = 'ABC123';
|
|
const request = validMeRequest(bearerToken);
|
|
|
|
Kolide.setBearerToken(bearerToken);
|
|
Kolide.me()
|
|
.then((user) => {
|
|
expect(user).toEqual(validUser);
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
})
|
|
.catch(done);
|
|
});
|
|
});
|
|
|
|
describe('#loginUser', () => {
|
|
it('calls the appropriate endpoint with the correct parameters', (done) => {
|
|
const request = validLoginRequest();
|
|
|
|
Kolide.loginUser({
|
|
username: 'admin',
|
|
password: 'secret',
|
|
})
|
|
.then((user) => {
|
|
expect(user).toEqual(validUser);
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
})
|
|
.catch(done);
|
|
});
|
|
});
|
|
|
|
describe('#logout', () => {
|
|
it('calls the appropriate endpoint with the correct parameters', (done) => {
|
|
const bearerToken = 'ABC123';
|
|
const request = validLogoutRequest(bearerToken);
|
|
|
|
Kolide.setBearerToken(bearerToken);
|
|
Kolide.logout()
|
|
.then(() => {
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
})
|
|
.catch(done);
|
|
});
|
|
});
|
|
|
|
describe('#forgotPassword', () => {
|
|
it('calls the appropriate endpoint with the correct parameters when successful', (done) => {
|
|
const request = validForgotPasswordRequest();
|
|
const email = 'hi@thegnar.co';
|
|
|
|
Kolide.forgotPassword({ email })
|
|
.then(() => {
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
})
|
|
.catch(done);
|
|
});
|
|
|
|
it('return errors correctly for unsuccessful requests', (done) => {
|
|
const error = 'Something went wrong';
|
|
const request = invalidForgotPasswordRequest(error);
|
|
const email = 'hi@thegnar.co';
|
|
|
|
Kolide.forgotPassword({ email })
|
|
.then(done)
|
|
.catch(errorResponse => {
|
|
const { response } = errorResponse;
|
|
|
|
expect(response).toEqual({ error });
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#resetPassword', () => {
|
|
const newPassword = 'p@ssw0rd';
|
|
|
|
it('calls the appropriate endpoint with the correct parameters when successful', (done) => {
|
|
const passwordResetToken = 'password-reset-token';
|
|
const request = validResetPasswordRequest(newPassword, passwordResetToken);
|
|
const formData = {
|
|
new_password: newPassword,
|
|
password_reset_token: passwordResetToken,
|
|
};
|
|
|
|
Kolide.resetPassword(formData)
|
|
.then(() => {
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
})
|
|
.catch(done);
|
|
});
|
|
|
|
it('return errors correctly for unsuccessful requests', (done) => {
|
|
const error = 'Resource not found';
|
|
const passwordResetToken = 'invalid-password-reset-token';
|
|
const request = invalidResetPasswordRequest(newPassword, passwordResetToken, error);
|
|
const formData = {
|
|
new_password: newPassword,
|
|
password_reset_token: passwordResetToken,
|
|
};
|
|
|
|
Kolide.resetPassword(formData)
|
|
.then(done)
|
|
.catch(errorResponse => {
|
|
const { response } = errorResponse;
|
|
|
|
expect(response).toEqual({ error });
|
|
expect(request.isDone()).toEqual(true);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|