Skip to content
Snippets Groups Projects
Commit 684be9d5 authored by Angapay Divin's avatar Angapay Divin
Browse files

add tests form auth module

parent 7995940e
No related branches found
No related tags found
1 merge request!74Resolve "Tests client: Tester le module auth"
/**
* This file is part of Anis Client.
*
* @copyright Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import * as authActions from "./auth.actions"
import { UserProfile } from "./user-profile.model";
describe('auth actions', () => {
it('should create login action', () => {
let action = authActions.login({ redirectUri: 'test' });
expect(action).toBeTruthy();
expect(authActions.login.type).toEqual('[Auth] Login');
});
it('should create logout action', () => {
let action = authActions.logout();
expect(action).toBeTruthy();
expect(authActions.logout.type).toEqual('[Auth] Logout');
});
it('should create authSuccess action', () => {
let action = authActions.authSuccess();
expect(action).toBeTruthy();
expect(authActions.authSuccess.type).toEqual('[Auth] Auth Success');
});
it('should create loadUserProfileSuccess action', () => {
let userProfile: UserProfile = {
createdTimestamp: 10,
email: 'test@test.com',
emailVerified: true,
enabled: true,
firstName: 'test',
id: '',
lastName: 'test',
totp: false,
username: 'test'
}
let action = authActions.loadUserProfileSuccess({ userProfile });
expect(action).toBeTruthy();
expect(authActions.loadUserProfileSuccess.type).toEqual('[Auth] Load User Profile Success');
});
it('should create loadUserRolesSuccess action', () => {
let userRoles: string[] = ['test1', 'test2'];
let action = authActions.loadUserRolesSuccess({ userRoles });
expect(action).toBeTruthy();
expect(authActions.loadUserRolesSuccess.type).toEqual('[Auth] Load User Roles Success');
});
it('should create openEditProfile action', () => {
let action = authActions.openEditProfile();
expect(action).toBeTruthy();
expect(authActions.openEditProfile.type).toEqual('[Auth] Edit Profile');
});
})
\ No newline at end of file
/**
* This file is part of Anis Client.
*
* @copyright Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import * as authActions from './auth.actions';
import { provideMockActions } from '@ngrx/effects/testing';
import { Observable, of } from 'rxjs';
import { cold, hot } from 'jasmine-marbles';
import { AuthEffects } from './auth.effects';
import { AppConfigService } from '../app-config.service';
import { KeycloakService } from 'keycloak-angular';
describe('auth] AuthEffects', () => {
let actions = new Observable();
let effects: AuthEffects;
let appConfigService: AppConfigService;
let keycloakService: KeycloakService;
let router: Router;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthEffects,
AppConfigService,
{ provide: Router, useValue: { navigate: jest.fn() } },
KeycloakService,
provideMockActions(() => actions),
]
}).compileComponents();
effects = TestBed.inject(AuthEffects);
appConfigService = TestBed.inject(AppConfigService);
keycloakService = TestBed.inject(KeycloakService);
router = TestBed.inject(Router);
});
it('should be created', () => {
expect(effects).toBeTruthy();
});
it('login$ should call keycloak.login()', () => {
const action = authActions.login({ redirectUri: 'test' });
let spy = jest.spyOn(keycloakService, 'login');
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.login$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
});
it('logout$ should call keycloak.logout()', () => {
const action = authActions.logout();
let spy = jest.spyOn(keycloakService, 'logout');
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.logout$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
});
it('authSuccess$ should call keycloak.loadUserProfile()', () => {
keycloakService.loadUserProfile = jest.fn().mockImplementation(() => of({}));
keycloakService.getUserRoles = jest.fn().mockImplementation(() => ['test']);
const action = authActions.authSuccess();
actions = hot('-a', { a: action });
const expected = cold('-(bc)', {
b: authActions.loadUserProfileSuccess({ userProfile: {} }),
c: authActions.loadUserRolesSuccess({ userRoles: keycloakService.getUserRoles() }),
})
expect(effects.authSuccess$).toBeObservable(expected);
});
it('openEditProfile$ should call window.open()', () => {
const action = authActions.openEditProfile();
let spy = jest.spyOn(window, 'open');
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.openEditProfile$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
});
});
/**
* This file is part of Anis Client.
*
* @copyright Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { AuthModule } from "./auth.module"
describe('[auth] AuthModule', () => {
it('should test auth module', () => {
expect(AuthModule.name).toBe('AuthModule');
})
})
\ No newline at end of file
/**
* This file is part of Anis Client.
*
* @copyright Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import * as fromAuth from './auth.reducer';
import * as authActions from './auth.actions';
import { UserProfile } from './user-profile.model';
import { Action } from '@ngrx/store';
describe('[auth] auth reducer', () => {
it('unknown action should return the default state', () => {
const { initialState } = fromAuth;
const action = { type: 'Unknown' };
const state = fromAuth.authReducer(initialState, action);
expect(state).toBe(initialState);
});
it('authSuccess action should set isAuthenticated to true', () => {
const { initialState } = fromAuth;
const action = authActions.authSuccess();
const state = fromAuth.authReducer(initialState, action);
expect(state.isAuthenticated).toBe(true);
expect(state).not.toBe(initialState);
});
it('loadUserProfileSuccess action should set userProfile to new value', () => {
const { initialState } = fromAuth;
let userProfile: UserProfile = {
email: 'test',
emailVerified: true
}
const action = authActions.loadUserProfileSuccess({ userProfile });
const state = fromAuth.authReducer(initialState, action);
expect(state.userProfile).toEqual(userProfile);
expect(state).not.toBe(initialState);
});
it('loadUserRolesSuccess action should set userRoles to new value', () => {
const { initialState } = fromAuth;
const action = authActions.loadUserRolesSuccess({userRoles: ['test']});
const state = fromAuth.authReducer(initialState, action);
expect(state.userRoles).toEqual(['test']);
expect(state).not.toBe(initialState);
});
it('should get isAuthenticated', () => {
const action = {} as Action;
const state = fromAuth.authReducer(undefined, action);
expect(fromAuth.selectIsAuthenticated(state)).toEqual(false);
});
it('should get userProfile', () => {
const action = {} as Action;
const state = fromAuth.authReducer(undefined, action);
expect(fromAuth.selectUserProfile(state)).toEqual(null);
});
it('should getuserRoles', () => {
const action = {} as Action;
const state = fromAuth.authReducer(undefined, action);
expect(fromAuth.selectUserRoles(state)).toEqual([]);
});
});
\ No newline at end of file
/**
* This file is part of Anis Client.
*
* @copyright Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { TestBed } from '@angular/core/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { KeycloakEventType, KeycloakService } from 'keycloak-angular';
import { AppConfigService } from '../app-config.service';
import { initializeKeycloak } from './init.keycloak';
class MockKeycloakService extends KeycloakService {
constructor() {
super();
}
}
class MockAppConfigService extends AppConfigService {
constructor() {
super();
}
}
describe('[auth] initializeKeycloak', () => {
let store: MockStore;
let KeycloakService: MockKeycloakService = new MockKeycloakService();
let appConfigService: MockAppConfigService = new MockAppConfigService();
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
providers: [
provideMockStore({}),
{ provide: KeycloakService, useValue: {} },
{ provide: AppConfigService, useValue: { ...appConfigService, } }
]
})
store = TestBed.inject(MockStore);
});
it('should return Promise.resolve(true)', (done) => {
appConfigService.authenticationEnabled = false;
let result = initializeKeycloak(KeycloakService, store, appConfigService);
expect(result).toEqual(Promise.resolve(true));
done();
});
it('should dispatch logout action and return keycloak.init', async () => {
appConfigService.authenticationEnabled = true;
const KeycloakEventTy = 'KeycloakEventType'
Object.defineProperty(KeycloakService, KeycloakEventTy, { value: { type: KeycloakEventType.OnAuthLogout } });
let value = {
config: {},
initOptions: {},
loadUserProfileAtStartUp: true,
bearerExcludedUrls: ['test']
}
KeycloakService.init = jest.fn().mockImplementation(() => Promise.resolve(value));
let result = initializeKeycloak(KeycloakService, store, appConfigService);
expect(result).toEqual(Promise.resolve(value));
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment