Skip to content

Commit

Permalink
typescript ok for mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
jlguenego committed Apr 7, 2020
1 parent a720ab1 commit 59715fb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 68 deletions.
20 changes: 14 additions & 6 deletions lib/sspi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface HANDLE {}
*
* @interface Token
*/
export interface Token {}
export type Token = string;

export type InformationClass = 'TokenGroups';

Expand Down Expand Up @@ -133,6 +133,18 @@ export interface InitializeSecurityContextInput {
targetDataRep?: TargetDataRepMapFlag;
}

/**
* Input of function AcquireCredentialsHandle
*
* @export
* @interface AcquireCredHandleInput
*/
export interface AcquireCredHandleInput {
packageName: string;
authData?: UserCredential;
credentialUse?: CredentialUseFlag;
}

/**
* Input of AcceptSecurityContext function.
*
Expand Down Expand Up @@ -186,11 +198,7 @@ export interface Sspi {
* @returns {CredentialWithExpiry}
* @memberof Sspi
*/
AcquireCredentialsHandle(input: {
packageName: string;
authData?: UserCredential;
credentialUse?: CredentialUseFlag;
}): CredentialWithExpiry;
AcquireCredentialsHandle(input: AcquireCredHandleInput): CredentialWithExpiry;

/**
* This function must be used only by a client. Its purpose is to setup a client/server security context.
Expand Down
39 changes: 24 additions & 15 deletions test/client-server.spec.js → test/client-server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
const express = require('express');
const { sso } = require('node-expose-sspi');
const assert = require('assert').strict;
const debug = require('debug')('node-expose-sspi:test');
const util = require('util');
import express from 'express';
import { sso } from 'node-expose-sspi';
import a from 'assert';
const assert = a.strict;
import dbg from 'debug';
const debug = dbg('node-expose-sspi:test');

describe('ClientServer', function() {
describe('ClientServer', function () {
if (sso.isOnDomain() && sso.isActiveDirectoryReachable()) {
it('should return the right json', async function() {
it('should return the right json', async function () {
this.timeout(15000);
debug('start');
await sso.init();
debug('init completed');
const app = express();
app.use(sso.auth({ useOwner: true, useActiveDirectory: true, useCookies: false }));
app.use(
sso.auth({
useOwner: true,
useActiveDirectory: true,
useCookies: false,
})
);
app.use((req, res) => {
res.json({
sso: req.sso,
Expand All @@ -28,11 +35,11 @@ describe('ClientServer', function() {
const state = {
i: 0,
clientsNbr: 0,
resolve: undefined,
increment() {
resolve: (): void => {},
increment(): void {
this.clientsNbr++;
},
decrement() {
decrement(): void {
this.clientsNbr--;
if (this.clientsNbr === 0 && this.i >= TIMES - 1) {
debug('about to close');
Expand All @@ -46,11 +53,13 @@ describe('ClientServer', function() {
},
};

async function simulateClient(i) {
async function simulateClient(i: number): Promise<void> {
try {
debug('start client', i);
state.increment();
const response = await new sso.Client().fetch('http://localhost:3000');
const response = await new sso.Client().fetch(
'http://localhost:3000'
);
const json = await response.json();
state.decrement();
assert(json.sso.user, 'json.sso.user should be truthy');
Expand All @@ -68,8 +77,8 @@ describe('ClientServer', function() {
await sso.sleep(DELAY);
}

function clean() {
return new Promise((resolve, reject) => {
function clean(): Promise<void> {
return new Promise((resolve) => {
state.resolve = resolve;
});
}
Expand Down
14 changes: 8 additions & 6 deletions test/sso.spec.js → test/sso.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const assert = require('assert');
const os = require('os');
const { sso, sysinfo } = require('node-expose-sspi');
const debug = require('debug')('node-expose-sspi:test');
import assert from 'assert';
import os from 'os';
import { sso, sysinfo } from 'node-expose-sspi';
import dbg from 'debug';

const debug = dbg('node-expose-sspi:test');

describe('SSO Unit Test', function () {
it('should test getDefaultDomain', function () {
Expand Down Expand Up @@ -31,12 +33,12 @@ describe('SSO Unit Test', function () {
await sso.getUser(`sAMAccountName=${os.userInfo().username}`);
});

it('should test sso.mutex', async function () {
it('should test sso.mutex', function () {
this.timeout(15000);
const mutex = new sso.Mutex();
const sleep = sso.sleep;

async function doSomething(label) {
async function doSomething(label: number): Promise<void> {
const release = await mutex.acquire();
debug(label, 'start');
for (let i = 0; i < 10; i++) {
Expand Down
66 changes: 34 additions & 32 deletions test/sspi.spec.js → test/sspi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
const { sspi, sso } = require('node-expose-sspi');
const os = require('os');
const assert = require('assert').strict;

describe('SSPI Unit Test', function() {
it('should return hello', function() {
import { sspi, sso, AcquireCredHandleInput } from 'node-expose-sspi';
import os from 'os';
import a from 'assert';
import { CredentialWithExpiry, ServerSecurityContext, SecurityContext, InitializeSecurityContextInput, AcceptSecurityContextInput, Token } from '../lib/sspi';
const assert = a.strict;

describe('SSPI Unit Test', function () {
it('should return hello', function () {
const result = sspi.hello();
assert.equal(result, 'Coucou JL!!!');
});

it('should test EnumerateSecurityPackages', function() {
it('should test EnumerateSecurityPackages', function () {
const securityPackages = sspi.EnumerateSecurityPackages();
assert(securityPackages instanceof Array);
assert(securityPackages[0].Comment);
});

it('should test QuerySecurityPackageInfo', function() {
it('should test QuerySecurityPackageInfo', function () {
const packageInfo = sspi.QuerySecurityPackageInfo('Negotiate');
assert(packageInfo);
assert.equal(packageInfo.Name, 'Negotiate');
});

const acquireCredentialsHandleClientInput = {
const acquireCredentialsHandleClientInput: AcquireCredHandleInput = {
packageName: 'Negotiate',
credentialUse: 'SECPKG_CRED_OUTBOUND',
};
Expand All @@ -33,33 +35,33 @@ describe('SSPI Unit Test', function() {
};
}

it('should test AcquireCredentialsHandle for client', function() {
it('should test AcquireCredentialsHandle for client', function () {
const clientCred = sspi.AcquireCredentialsHandle(
acquireCredentialsHandleClientInput
);
assert(clientCred);
assert(clientCred.credential);
});

const acquireCredentialsHandleServerInput = {
const acquireCredentialsHandleServerInput: AcquireCredHandleInput = {
packageName: 'Negotiate',
credentialUse: 'SECPKG_CRED_INBOUND',
};

it('should test AcquireCredentialsHandle for server', function() {
const serverCred = sspi.AcquireCredentialsHandle(
it('should test AcquireCredentialsHandle for server', function () {
const sc = sspi.AcquireCredentialsHandle(
acquireCredentialsHandleServerInput
);
assert(serverCred);
assert(serverCred.credential);
assert(sc);
assert(sc.credential);
});

let serverCred;
let username;
let serverSecurityContext;
let clientSecurityContext;
let serverCred: CredentialWithExpiry;
let username: string;
let serverSecurityContext: ServerSecurityContext;
let clientSecurityContext: SecurityContext;

it('should test creating a security context', function() {
it('should test creating a security context', function () {
const packageInfo = sspi.QuerySecurityPackageInfo('Negotiate');
const clientCred = sspi.AcquireCredentialsHandle(
acquireCredentialsHandleClientInput
Expand All @@ -68,7 +70,7 @@ describe('SSPI Unit Test', function() {
acquireCredentialsHandleServerInput
);

const input = {
const input: InitializeSecurityContextInput = {
credential: clientCred.credential,
targetName: 'kiki',
cbMaxToken: packageInfo.cbMaxToken,
Expand All @@ -80,7 +82,7 @@ describe('SSPI Unit Test', function() {
clientSecurityContext.SecBufferDesc.buffers[0] instanceof ArrayBuffer
);

const serverInput = {
const serverInput: AcceptSecurityContextInput = {
credential: serverCred.credential,
clientSecurityContext,
contextReq: ['ASC_REQ_CONNECTION'],
Expand Down Expand Up @@ -150,14 +152,14 @@ describe('SSPI Unit Test', function() {
assert(wentInCatch);
});

it('should test LookupAccountName', function() {
it('should test LookupAccountName', function () {
const sidObject = sspi.LookupAccountName(username);
assert(sidObject instanceof Object);
assert(sidObject.domain);
assert(sidObject.sid);
});

it('should test GetUserName', function() {
it('should test GetUserName', function () {
const username2 = sspi.GetUserName();
// on Domain, username2='Guest'
if (sso.isOnDomain()) {
Expand All @@ -167,7 +169,7 @@ describe('SSPI Unit Test', function() {
}
});

it('should test QueryCredentialsAttributes', function() {
it('should test QueryCredentialsAttributes', function () {
const attributes = sspi.QueryCredentialsAttributes(
serverCred.credential,
'SECPKG_CRED_ATTR_NAMES'
Expand All @@ -176,7 +178,7 @@ describe('SSPI Unit Test', function() {
assert(attributes.sUserName);
});

it('should test QueryContextAttributes', function() {
it('should test QueryContextAttributes', function () {
const names = sspi.QueryContextAttributes(
serverSecurityContext.contextHandle,
'SECPKG_ATTR_NAMES'
Expand All @@ -185,22 +187,22 @@ describe('SSPI Unit Test', function() {
assert(names.sUserName);
});

let accessToken;
it('should test QuerySecurityContextToken', function() {
let accessToken: Token;
it('should test QuerySecurityContextToken', function () {
accessToken = sspi.QuerySecurityContextToken(
serverSecurityContext.contextHandle
);
assert(accessToken);
assert(accessToken.startsWith('0x'));
});

it('should test GetTokenInformation', function() {
it('should test GetTokenInformation', function () {
const groups = sspi.GetTokenInformation(accessToken, 'TokenGroups');
assert(groups instanceof Array);
assert(typeof groups[0] === 'string');
});

it('should test CloseHandle', function() {
it('should test CloseHandle', function () {
sspi.CloseHandle(accessToken);
let wentInCatch = false;
try {
Expand All @@ -211,7 +213,7 @@ describe('SSPI Unit Test', function() {
assert(wentInCatch);
});

it('should test DeleteSecurityContext', function() {
it('should test DeleteSecurityContext', function () {
sspi.DeleteSecurityContext(serverSecurityContext.contextHandle);
sspi.DeleteSecurityContext(clientSecurityContext.contextHandle);
let wentInCatch = false;
Expand All @@ -223,7 +225,7 @@ describe('SSPI Unit Test', function() {
assert(wentInCatch);
});

it('should test FreeCredentialsHandle', function() {
it('should test FreeCredentialsHandle', function () {
sspi.FreeCredentialsHandle(serverCred.credential);
sspi.FreeCredentialsHandle(serverCred.credential);
});
Expand Down
9 changes: 0 additions & 9 deletions test/sysinfo.spec.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/sysinfo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sysinfo } from 'node-expose-sspi';
import a from 'assert';
const assert = a.strict;

describe('SYSINFO Unit Test', function () {
it('should test GetComputerNameEx', function () {
const str = sysinfo.GetComputerNameEx('ComputerNameDnsDomain');
assert(typeof str === 'string');
});
});

0 comments on commit 59715fb

Please sign in to comment.