Skip to content

Commit

Permalink
Added specs and implementation for online resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-KaiNet committed May 4, 2016
1 parent 8e7db3e commit f10b315
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"merge-stream": "^1.0.0",
"mocha-junit-reporter": "^1.11.1",
"mockery": "^1.6.2",
"node-spoauth": "^1.0.0",
"remap-istanbul": "^0.6.3",
"request": "^2.72.0",
"request-promise": "^3.0.0",
Expand Down
8 changes: 3 additions & 5 deletions src/core/SPRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import * as requestp from 'request-promise';
import * as Promise from 'bluebird';

import {IUserCredentials} from './interfaces/IUserCredentials';
import {IOAuthCredentials} from './interfaces/IOAuthCredentials';
import {IEnvironment} from './interfaces/IEnvironment';
import {AuthResolverFactory} from './auth/AuthResolverFactory';
import {ISPRequest} from './interfaces/ISPrequest';

export function create(credentials: IUserCredentials | IOAuthCredentials, environment?: IEnvironment): ISPRequest {
export function create(credentials: IUserCredentials, environment?: IEnvironment): ISPRequest {

let coreRequest: any = (options: OptionsWithUrl): Promise<IncomingMessage> => {
let requestDeferred: Promise.Resolver<IncomingMessage> = Promise.defer<IncomingMessage>();

let requestPromiseOptions: RequestPromiseOptions = <RequestPromiseOptions>options;
requestPromiseOptions.resolveWithFullResponse = true;
requestPromiseOptions.simple = false;
(<RequestPromiseOptions>options).resolveWithFullResponse = true;
(<RequestPromiseOptions>options).simple = true;

options.headers = options.headers || {};
if (!options.headers['Accept']) {
Expand Down
3 changes: 1 addition & 2 deletions src/core/auth/IAuthOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {OptionsWithUrl} from 'request';

import {IUserCredentials} from './../interfaces/IUserCredentials';
import {IOAuthCredentials} from './../interfaces/IOAuthCredentials';
import {IEnvironment} from './../interfaces/IEnvironment';

export interface IAuthOptions {
options: OptionsWithUrl;
credentials: IUserCredentials | IOAuthCredentials;
credentials: IUserCredentials;
env: IEnvironment;
}

5 changes: 5 additions & 0 deletions src/core/auth/OnPremResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class OnPremResolver implements IAuthResolver {

let type1msg: any = ntlm.createType1Message(ntlmOptions);

/* workaround for on premise self signed or not trusted certificates */
if (isHttps) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
}

rp({
url: this.authOptions.options.url,
method: 'GET',
Expand Down
24 changes: 23 additions & 1 deletion src/core/auth/OnlineResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {OptionsWithUrl} from 'request';
import * as Promise from 'bluebird';
import * as consts from 'constants';

let sp: any = require('node-spoauth');

import {IAuthResolver} from './IAuthResolver';
import {IAuthOptions} from './IAuthOptions';
Expand All @@ -9,6 +12,25 @@ export class OnlineResolver implements IAuthResolver {
constructor(private authOptions: IAuthOptions) { }

public ApplyAuthHeaders(): Promise<OptionsWithUrl> {
return null;
let deferred: Promise.Resolver<OptionsWithUrl> = Promise.defer<OptionsWithUrl>();

let service: any = new sp.RestService(this.authOptions.options.url);

let signin: (username: string, password: string) => Promise<any> =
Promise.promisify<any, string, string>(service.signin, { context: service });

signin(this.authOptions.credentials.username, this.authOptions.credentials.password)
.then((auth) => {
this.authOptions.options.headers = this.authOptions.options.headers || {};
this.authOptions.options.headers['Cookie'] = `FedAuth=${auth.FedAuth}; rtFa=${auth.rtFa}`;
this.authOptions.options['secureOptions'] = consts.SSL_OP_NO_TLSv1_2;

deferred.resolve(this.authOptions.options);
})
.catch((err) => {
deferred.reject(err);
});

return deferred.promise;
}
}
4 changes: 0 additions & 4 deletions src/core/interfaces/IOAuthCredentials.ts

This file was deleted.

90 changes: 90 additions & 0 deletions test/unit/OnlineResolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {expect} from 'chai';
import * as sinon from 'sinon';
import {SinonStub} from 'sinon';
import * as consts from 'constants';

import {IAuthOptions} from './../../src/core/auth/IAuthOptions';
import {IUserCredentials} from './../../src/core/interfaces/IUserCredentials';
import {OnlineResolver} from './../../src/core/auth/OnlineResolver';

let creds: IUserCredentials = {
username: 'user',
password: 'pass'
};

let onlineOptions: IAuthOptions = {
credentials: creds,
options: {
url: 'https://sp2013dev.sharepoint.com/sites/dev/_api'
},
env: undefined
};

let authResponse: any = {
FedAuth: 'fedauth',
rtFa: 'rtfa'
};

let cookieString: string = `FedAuth=${authResponse.FedAuth}; rtFa=${authResponse.rtFa}`;

describe('sp-request: OnlineResolver tests', () => {
let resolver: OnlineResolver;
let siginStup: SinonStub;

beforeEach(() => {
let resolverModule: any = require('./../../src/core/auth/OnlineResolver');
resolver = new resolverModule.OnlineResolver(onlineOptions);
let sp: any = require('node-spoauth');
siginStup = sinon.stub(sp.RestService.prototype, 'signin');
});

afterEach(() => {
siginStup.restore();
});

it('should set cookie header', (done) => {
siginStup.callsArgWith(2, null, {
FedAuth: 'fedauth',
rtFa: 'rtfa'
});

resolver.ApplyAuthHeaders()
.then((options) => {
expect(options.headers['Cookie']).to.equal(cookieString);
done();
})
.catch((err) => {
done(err);
});
});

it('should set secureOptions', (done) => {
siginStup.callsArgWith(2, null, {
FedAuth: 'fedauth',
rtFa: 'rtfa'
});

resolver.ApplyAuthHeaders()
.then((options) => {
expect(options['secureOptions']).to.equal(consts.SSL_OP_NO_TLSv1_2);
done();
})
.catch((err) => {
done(err);
});
});

it('should throws en error', (done) => {
let error: string = 'error';
siginStup.throws(error);

resolver.ApplyAuthHeaders()
.then((options) => {
//
})
.catch((err) => {
expect(err.name).to.equal(error);
done();
});
});
});
1 change: 1 addition & 0 deletions test/unit/tests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './SPRequest.spec';
import './AuthResolverFactory.spec';
import './OnPremResolver.spec';
import './OnlineResolver.spec';

0 comments on commit f10b315

Please sign in to comment.