Skip to content

Commit

Permalink
Test auth guards (#225)
Browse files Browse the repository at this point in the history
* router structure changed

* Fix relative paths

* Login route guards moved to separate components

* Tests for loginById added

* loginByToken tests added

* Remove unneeded stubActions option
  • Loading branch information
brachkow committed Mar 6, 2023
1 parent 480fd37 commit 0e16b54
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/axios/onResponseRejected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('custom axios', () => {

beforeEach(() => {
const app = createApp({});
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
const pinia = createTestingPinia({ createSpy: vi.fn });
app.use(pinia);
setActivePinia(pinia);

Expand Down
21 changes: 7 additions & 14 deletions src/router.ts → src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
import useAuth from '@/stores/auth';
import useUser from '@/stores/user';
import useStudies from '@/stores/studies';
import useMaterials from './stores/materials';
import useDiplomas from './stores/diplomas';
import useLoading from './stores/loading';
import useMaterials from '@/stores/materials';
import useDiplomas from '@/stores/diplomas';
import useLoading from '@/stores/loading';
const VHomeView = () => import('@/views/VHomeView.vue');
const VMailSentView = () => import('@/views/VMailSentView.vue');
const VSettingsView = () => import('@/views/VSettingsView.vue');
Expand All @@ -21,6 +21,8 @@ const VHomeworkAnswerView = () => import('@/views/VHomeworkAnswerView.vue');
const VLoginResetView = () => import('@/views/VLoginResetView.vue');
const VLoginChangeView = () => import('@/views/VLoginChangeView.vue');
const VCertificatesView = () => import('@/views/VCertificatesView.vue');
import loginByToken from '@/router/loginByToken';
import loginById from '@/router/loginById';

const isAuthorized = () => {
const auth = useAuth();
Expand Down Expand Up @@ -90,11 +92,7 @@ export const routes = [
path: '/auth/passwordless/:passwordlessToken',
name: 'token',
component: VLoadingView,
beforeEnter: async (to: RouteLocationNormalized) => {
const auth = useAuth();
await auth.exchangeTokens(String(to.params.passwordlessToken));
return { name: 'home' };
},
beforeEnter: [loginByToken],
meta: {
isPublic: true,
},
Expand All @@ -103,12 +101,7 @@ export const routes = [
path: '/auth/as/:userId',
name: 'auth-as',
component: VLoadingView,
beforeEnter: async (to: RouteLocationNormalized) => {
const auth = useAuth();

await auth.loginWithUserId(String(to.params.userId));
return { name: 'home' };
},
beforeEnter: [loginById],
},
{
path: '/materials/:id',
Expand Down
42 changes: 42 additions & 0 deletions src/router/loginById.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { faker } from '@faker-js/faker';
import { describe, test, beforeEach, vi, expect } from 'vitest';
import loginById from './loginById';
import useAuth from '@/stores/auth';
import type { RouteLocationNormalized } from 'vue-router';
import { createApp } from 'vue';
import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';

const userId = faker.datatype.uuid();

const to: Partial<RouteLocationNormalized> = {
params: {
userId,
},
};

describe('loginById', () => {
let auth: ReturnType<typeof useAuth>;

beforeEach(() => {
const app = createApp({});
const pinia = createTestingPinia({ createSpy: vi.fn });
app.use(pinia);
setActivePinia(pinia);

auth = useAuth();
});

test('should call loginWithUserId', async () => {
await loginById(to as RouteLocationNormalized);

expect(auth.loginWithUserId).toHaveBeenCalled();
expect(auth.loginWithUserId).toHaveBeenCalledWith(userId);
});

test('should return directions to home', async () => {
expect(await loginById(to as RouteLocationNormalized)).toStrictEqual({
name: 'home',
});
});
});
11 changes: 11 additions & 0 deletions src/router/loginById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { RouteLocationNormalized } from 'vue-router';
import useAuth from '@/stores/auth';

const loginByToken = async (to: RouteLocationNormalized) => {
const auth = useAuth();
await auth.loginWithUserId(String(to.params.userId));

return { name: 'home' };
};

export default loginByToken;
42 changes: 42 additions & 0 deletions src/router/loginByToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { faker } from '@faker-js/faker';
import { describe, test, beforeEach, vi, expect } from 'vitest';
import loginByToken from './loginByToken';
import useAuth from '@/stores/auth';
import type { RouteLocationNormalized } from 'vue-router';
import { createApp } from 'vue';
import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';

const passwordlessToken = faker.datatype.uuid();

const to: Partial<RouteLocationNormalized> = {
params: {
passwordlessToken,
},
};

describe('loginByToken', () => {
let auth: ReturnType<typeof useAuth>;

beforeEach(() => {
const app = createApp({});
const pinia = createTestingPinia({ createSpy: vi.fn });
app.use(pinia);
setActivePinia(pinia);

auth = useAuth();
});

test('should call exchangeTokens', async () => {
await loginByToken(to as RouteLocationNormalized);

expect(auth.exchangeTokens).toHaveBeenCalled();
expect(auth.exchangeTokens).toHaveBeenCalledWith(passwordlessToken);
});

test('should return directions to home', async () => {
expect(await loginByToken(to as RouteLocationNormalized)).toStrictEqual({
name: 'home',
});
});
});
10 changes: 10 additions & 0 deletions src/router/loginByToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { RouteLocationNormalized } from 'vue-router';
import useAuth from '@/stores/auth';

const loginByToken = async (to: RouteLocationNormalized) => {
const auth = useAuth();
await auth.exchangeTokens(String(to.params.passwordlessToken));
return { name: 'home' };
};

export default loginByToken;

0 comments on commit 0e16b54

Please sign in to comment.