Commit ca524b3c authored by Eduardo Sanz García's avatar Eduardo Sanz García
Browse files

feat: added initial tests and CI configuration

parent 93df732d
......@@ -37,15 +37,27 @@
"lint": [
{
"project": "src/tsconfig.app.json",
"exclude": "**/node_modules/**"
"exclude": [
"**/node_modules/**",
"**/testing/*",
"**/nolint/*"
]
},
{
"project": "src/tsconfig.spec.json",
"exclude": "**/node_modules/**"
"exclude": [
"**/node_modules/**",
"**/testing/*",
"**/nolint/*"
]
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": "**/node_modules/**"
"exclude": [
"**/node_modules/**",
"**/testing/*",
"**/nolint/*"
]
}
],
"test": {
......
......@@ -8,7 +8,6 @@ module.exports = function (config) {
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
......@@ -22,12 +21,20 @@ module.exports = function (config) {
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml'],
reporters: ['progress', 'coverage-istanbul'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
browsers: ['ChromiumHeadlessNoSandbox'],
customLaunchers: {
ChromiumHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: [
'--no-sandbox'
]
}
},
singleRun: false
});
};
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
......@@ -6,8 +6,9 @@
"ng": "ng",
"start": "ng serve --aot",
"build": "ng build --prod --aot",
"test": "ng test",
"lint": "ng lint",
"test": "ng test --config=karma_chrome.conf.js",
"test:sr": "ng test --progress=false --single-run=true --code-coverage=true",
"lint": "ng lint --type-check",
"e2e": "ng e2e",
"packagr": "ng-packagr -p ng-package.json"
},
......@@ -15,7 +16,11 @@
"keywords": [
"angular",
"jwt",
"authentication"
"authentication",
"elixir",
"ebi",
"aap",
"aai"
],
"repository": {
"type": "git",
......
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import {
TestBed,
async
} from '@angular/core/testing';
import {
fakeAsync,
flushMicrotasks
} from '@angular/core/testing';
import {
AppComponent
} from './app.component';
import {
AuthModule
} from './modules/auth/auth.module';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
}));
beforeEach(async (() => {
TestBed.configureTestingModule({
imports: [
AuthModule.forRoot()
],
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async (() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should not be authenticated`, async (() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
app.isAuthenticated.subscribe((result: any) => {
expect(result).toEqual('false');
});
}));
it('should render title in a h1 tag', async (() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Auth testing app');
}));
});
......@@ -5,6 +5,13 @@ import {
ModuleWithProviders,
} from '@angular/core';
import {
JWT_OPTIONS,
JwtHelperService
} from '@auth0/angular-jwt';
import {
TokenService
} from './token.service';
import {
AuthConfig,
AAP_CONFIG,
......@@ -13,14 +20,6 @@ import {
import {
AuthService
} from './auth.service';
import {
TokenService
} from './token.service';
import {
JWT_OPTIONS,
JwtHelperService
} from '@auth0/angular-jwt';
@NgModule({})
export class AuthModule {
......@@ -35,12 +34,6 @@ export class AuthModule {
return {
ngModule: AuthModule,
providers: [{
provide: AAP_CONFIG,
useValue: options ? options : DEFAULT_CONF
},
AuthService,
TokenService,
{
provide: JWT_OPTIONS,
useValue: options ? {
tokenGetter: options.tokenGetter
......@@ -48,7 +41,13 @@ export class AuthModule {
tokenGetter: DEFAULT_CONF.tokenGetter
}
},
JwtHelperService
JwtHelperService,
TokenService,
{
provide: AAP_CONFIG,
useValue: options ? options : DEFAULT_CONF
},
AuthService
]
};
}
......
import { TestBed, inject } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { spyOnClass } from 'jasmine-es6-spies';
import { AuthConfig } from './index';
import { AuthService } from './auth.service';
import { TokenService } from './token.service';
class APP_CONFIG implements AuthConfig{
authURL: "something";
}
class DummyComponent {}
describe('Service: Auth', () => {
// secret sauce which we inject into the tests
const tokenerSpy = spyOnClass(TokenService);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
{ path: '', component: DummyComponent }
])
],
providers: [
APP_CONFIG,
{ provide: TokenService, useValue: tokenerSpy },
AuthService
]
import {
TestBed,
inject
} from '@angular/core/testing';
import {
AuthService
} from './auth.service';
import {
TokenService
} from './token.service';
import {
JWT_OPTIONS,
JwtHelperService
} from '@auth0/angular-jwt';
import {
VALID_TOKEN,
EXPIRED_TOKEN
} from 'app/../../testing/tokens';
import {
AAP_CONFIG,
DEFAULT_CONF
} from './auth.config';
describe('AuthService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
provide: JWT_OPTIONS,
useValue: {
tokenGetter: () => VALID_TOKEN
}
},
JwtHelperService,
TokenService,
{
provide: AAP_CONFIG,
useValue: DEFAULT_CONF
},
AuthService
]
});
});
});
it('should ...', inject([AuthService], (service: AuthService) => {
expect(service).toBeTruthy();
}));
it('should check token to see if user is logged in',
inject([AuthService], (service: AuthService) => {
tokenerSpy.isTokenExpired.and.returnValues(true, false);
expect(service.loggedIn()).toBeFalsy();
expect(service.loggedIn()).toBeTruthy();
expect(tokenerSpy.isTokenExpired).toHaveBeenCalledTimes(2);
}));
it('should be created', inject([AuthService], (service: AuthService) => {
expect(service).toBeTruthy();
}));
});
import { TestBed, inject } from '@angular/core/testing';
import {
TestBed,
inject
} from '@angular/core/testing';
import { TokenService } from './token.service';
import {
TokenService
} from './token.service';
import {
JWT_OPTIONS,
JwtHelperService
} from '@auth0/angular-jwt';
import {
VALID_TOKEN,
EXPIRED_TOKEN
} from 'app/../../testing/tokens';
describe('TokenService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [TokenService]
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
provide: JWT_OPTIONS,
useValue: {
tokenGetter: () => VALID_TOKEN
}
},
JwtHelperService,
TokenService
]
});
});
});
it('should be created', inject([TokenService], (service: TokenService) => {
expect(service).toBeTruthy();
}));
it('should be created', inject([TokenService], (service: TokenService) => {
expect(service).toBeTruthy();
}));
});
export const EXPIRED_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjE1MTgwODM0MzMsImp0aSI6InR6Wmo4Z29xUWVMRVBNakxIMDJwVEEiLCJpYXQiOjE1MTgwODMzNzMsInN1YiI6InVzci03NWY0YjAwMCIsImVtYWlsIjoidGVzdEBlYmkuYWMudWsiLCJuaWNrbmFtZSI6IjZmMzdhMGJlYjdiMTZmMzdhMGJlYjdiMWIiLCJuYW1lIjoiRWQgTXVuZGVuIEdyYXMiLCJkb21haW5zIjpbImFhcC11c2Vycy1kb21haW4iXX0.JrIBLqSmiZixbKfvutQKdx3b_O1SSjssL5mfyVks6Aw';
export const VALID_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjEwMDAwMDAwMDAwMDAwMDAwMDAwMCwianRpIjoidHpaajhnb3FRZUxFUE1qTEgwMnBUQSIsImlhdCI6MTUxODA4MzM3Mywic3ViIjoidXNyLTc1ZjRiMDAwIiwiZW1haWwiOiJ0ZXN0QGViaS5hYy51ayIsIm5pY2tuYW1lIjoiNmYzN2EwYmViN2IxNmYzN2EwYmViN2IxYiIsIm5hbWUiOiJFZCBNdW5kZW4gR3JhcyIsImRvbWFpbnMiOlsiYWFwLXVzZXJzLWRvbWFpbiJdfQ.NfvjfYjVQy9BIL2jqFOyHTDXXEVlnf33LVzgccljsc0';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment