"use strict" ; import "reflect-metadata" ; import { anything , instance , mock , verify , when } from "ts-mockito" ; import { WinPersistentCredentialsStore } from "../../../../src/bll/credentialsstore/win32/win-credstore" ; import { assert } from "chai" ; import { WinCredStoreParsingStream , WinCredStoreParsingStreamWrapper } from "../../../../src/bll/credentialsstore/win32/win-credstore-parser" ; import { CpProxy } from "../../../../src/bll/moduleproxies/cp-proxy" ; import { TestSettings } from "../../../testsettings" ; import * as stream from "stream" ; suite ( "WindowsCredentialStoreApi" , ( ) => { test ( "should verify getCredentialsListStream" , function ( ) { const readable = new stream . Readable ( { objectMode : true } ) ; const cpMock = mock ( CpProxy ) ; when ( cpMock . spawn ( anything ( ) , anything ( ) ) ) . thenReturn ( { stdout : readable } ) ; readable . push ( null ) ; const cpSpy = instance ( cpMock ) ; const wrapperMock = mock ( WinCredStoreParsingStreamWrapper ) ; when ( wrapperMock . parser ( ) ) . thenReturn ( new WinCredStoreParsingStream ( ) ) ; const wrapperSpy = instance ( wrapperMock ) ; const winCredStore = new WinPersistentCredentialsStore ( wrapperSpy , cpSpy ) ; winCredStore . getCredentialsListStream ( ) ; verify ( cpMock . spawn ( anything ( ) , anything ( ) ) ) . called ( ) ; } ) ; test ( "should verify getCredentialsListStream with real data" , function ( done ) { const readable = new stream . Readable ( { objectMode : true } ) ; const cpMock = mock ( CpProxy ) ; when ( cpMock . spawn ( anything ( ) , anything ( ) ) ) . thenReturn ( { stdout : readable } ) ; readable . push ( TestSettings . winCredentialsExample ) ; readable . push ( null ) ; const cpSpy = instance ( cpMock ) ; const wrapperMock = mock ( WinCredStoreParsingStreamWrapper ) ; when ( wrapperMock . parser ( ) ) . thenReturn ( new WinCredStoreParsingStream ( ) ) ; const wrapperSpy = instance ( wrapperMock ) ; const winCredStore = new WinPersistentCredentialsStore ( wrapperSpy , cpSpy ) ; const credsStream = winCredStore . getCredentialsListStream ( ) ; const creds = [ ] ; credsStream . on ( "data" , ( cred ) => { creds . push ( cred ) ; } ) ; credsStream . on ( "end" , ( ) => { const actualCreds = new Buffer ( creds [ 0 ] . credential , "hex" ) . toString ( "utf8" ) ; const expectedCreds = TestSettings . winCredentials . credential . toString ( "utf8" ) ; assert . equal ( actualCreds , expectedCreds ) ; assert . equal ( creds [ 0 ] . targetName , TestSettings . persistentCredentialsPrefix + TestSettings . winCredentials . targetName ) ; done ( ) ; } ) ; } ) ; test ( "should verify set" , function ( done ) { const cpMock = mock ( CpProxy ) ; const cpSpy = instance ( cpMock ) ; const wrapperMock = mock ( WinCredStoreParsingStreamWrapper ) ; const wrapperSpy = instance ( wrapperMock ) ; const winCredStore = new WinPersistentCredentialsStore ( wrapperSpy , cpSpy ) ; winCredStore . set ( TestSettings . url + "|" + TestSettings . account , TestSettings . password ) . then ( ( ) => { verify ( cpMock . execFileAsync ( anything ( ) , anything ( ) ) ) . called ( ) ; done ( ) ; } ) . catch ( ( err ) => { done ( err ) ; } ) ; } ) ; test ( "should verify remove" , async function ( ) { const cpMock = mock ( CpProxy ) ; const cpSpy = instance ( cpMock ) ; const wrapperMock = mock ( WinCredStoreParsingStreamWrapper ) ; const wrapperSpy = instance ( wrapperMock ) ; const winCredStore = new WinPersistentCredentialsStore ( wrapperSpy , cpSpy ) ; await winCredStore . remove ( TestSettings . url + "|" + TestSettings . account ) ; verify ( cpMock . execFileAsync ( anything ( ) , anything ( ) ) ) . called ( ) ; } ) ; } ) ;