Files
directus/src/auth.test.ts
dependabot-preview[bot] 26960961e8 Bump prettier from 1.19.1 to 2.0.2 (#239)
* Bump prettier from 1.19.1 to 2.0.2

Bumps [prettier](https://github.com/prettier/prettier) from 1.19.1 to 2.0.2.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/1.19.1...2.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Add dangling comma's

* Update eslint config to match prettier default

* Make eslint match prettier for real this time

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
2020-03-24 10:51:30 -04:00

152 lines
4.4 KiB
TypeScript

import Vue from 'vue';
import VueCompositionAPI from '@vue/composition-api';
import api from '@/api';
import { checkAuth, login, logout, LogoutReason } from './auth';
import { useProjectsStore } from '@/stores/projects';
import router from '@/router';
import { hydrate, dehydrate } from '@/hydrate';
jest.mock('@/api');
jest.mock('@/router');
jest.mock('@/hydrate');
describe('Auth', () => {
beforeAll(() => {
Vue.config.productionTip = false;
Vue.config.devtools = false;
Vue.use(VueCompositionAPI);
});
beforeEach(() => {
jest.spyOn(api, 'get');
jest.spyOn(api, 'post');
});
describe('checkAuth', () => {
it('Does not ping the API if the curent project key is null', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = null;
(api.get as jest.Mock).mockImplementation(() =>
Promise.resolve({ data: { data: { authenticated: true } } })
);
await checkAuth();
expect(api.get).not.toHaveBeenCalled();
});
it('Calls the api with the correct endpoint', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
(api.get as jest.Mock).mockImplementation(() =>
Promise.resolve({ data: { data: { authenticated: true } } })
);
await checkAuth();
expect(api.get).toHaveBeenCalledWith('/test-project/auth/check');
});
it('Returns true if user is logged in', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
(api.get as jest.Mock).mockImplementation(() =>
Promise.resolve({ data: { data: { authenticated: true } } })
);
const loggedIn = await checkAuth();
expect(loggedIn).toBe(true);
});
it('Returns false if user is logged out', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
(api.get as jest.Mock).mockImplementation(() =>
Promise.resolve({ data: { data: { authenticated: false } } })
);
const loggedIn = await checkAuth();
expect(loggedIn).toBe(false);
});
});
describe('login', () => {
it('Calls /auth/authenticate with the provided credentials', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
await login({ email: 'test', password: 'test' });
expect(api.post).toHaveBeenCalledWith('/test-project/auth/authenticate', {
mode: 'cookie',
email: 'test',
password: 'test',
});
});
it('Calls hydrate on successful login', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
await login({ email: 'test', password: 'test' });
expect(hydrate).toHaveBeenCalled();
});
});
describe('logout', () => {
it('Does not do anything when there is no current project', async () => {
useProjectsStore({});
await logout();
expect(dehydrate).not.toHaveBeenCalled();
});
it('Calls dehydrate', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
await logout();
expect(dehydrate).toHaveBeenCalled();
});
it('Posts to /logout on regular sign out', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'test-project';
await logout();
expect(api.post).toHaveBeenCalledWith('/test-project/auth/logout');
});
it('Navigates to the current projects login page', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'my-project';
await logout();
expect(router.push).toHaveBeenCalledWith({
path: '/my-project/login',
query: {
reason: LogoutReason.SIGN_OUT,
},
});
});
it('Does not navigate when the navigate option is false', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'my-project';
await logout({ navigate: false });
expect(router.push).not.toHaveBeenCalled();
});
it('Adds the reason query param if any non-default reason is given', async () => {
const projectsStore = useProjectsStore({});
projectsStore.state.currentProjectKey = 'my-project';
await logout({ reason: LogoutReason.ERROR_SESSION_EXPIRED });
expect(router.push).toHaveBeenCalledWith({
path: '/my-project/login',
query: {
reason: LogoutReason.ERROR_SESSION_EXPIRED,
},
});
});
});
});