import * as assert from "assert" ; import * as Azure from "azure-storage" ; import { configLogger } from "../../../src/common/Logger" ; import TableServer from "../../../src/table/TableServer" ; import { HeaderConstants , TABLE_API_VERSION } from "../../../src/table/utils/constants" ; import { EMULATOR_ACCOUNT_NAME , getUniqueName , overrideRequest , restoreBuildRequestOptions } from "../../testutils" ; import { HOST , PROTOCOL , PORT , createConnectionStringForTest , createTableServerForTest } from "./table.entity.test.utils" ; configLogger ( false ) ; const testLocalAzuriteInstance = true ; describe ( "table APIs test" , ( ) => { let server ; const tableService = Azure . createTableService ( createConnectionStringForTest ( testLocalAzuriteInstance ) ) ; tableService . enableGlobalHttpAgent = true ; let tableName = getUniqueName ( "table" ) ; const requestOverride = { headers : { } } ; before ( async ( ) => { overrideRequest ( requestOverride , tableService ) ; server = createTableServerForTest ( ) ; tableName = getUniqueName ( "table" ) ; await server . start ( ) ; } ) ; after ( async ( ) => { restoreBuildRequestOptions ( tableService ) ; tableService . removeAllListeners ( ) ; await server . close ( ) ; } ) ; it ( "createTable, prefer=return-no-content, accept=application/json;odata=minimalmetadata @loki" , ( done ) => { requestOverride . headers = { Prefer : "return-no-content" , accept : "application/json;odata=minimalmetadata" } ; tableService . createTable ( tableName , ( error , result , response ) => { if ( ! error ) { assert . strictEqual ( result . TableName , tableName ) ; assert . strictEqual ( result . statusCode , 204 ) ; const headers = response . headers ! ; assert . strictEqual ( headers [ "x-ms-version" ] , TABLE_API_VERSION ) ; assert . deepStrictEqual ( response . body , "" ) ; } done ( ) ; } ) ; } ) ; it ( "createTable, prefer=return-content, accept=application/json;odata=fullmetadata @loki" , ( done ) => { requestOverride . headers = { Prefer : "return-content" , accept : "application/json;odata=fullmetadata" } ; tableService . createTable ( tableName , ( error , result , response ) => { if ( ! error ) { assert . strictEqual ( result . TableName , tableName ) ; assert . strictEqual ( result . statusCode , 201 ) ; const headers = response . headers ! ; assert . strictEqual ( headers [ "x-ms-version" ] , TABLE_API_VERSION ) ; const bodies = response . body ! as any ; assert . deepStrictEqual ( bodies . TableName , tableName ) ; assert . deepStrictEqual ( bodies [ "odata.type" ] , `${ EMULATOR_ACCOUNT_NAME }.Tables` ) ; assert . deepStrictEqual ( bodies [ "odata.metadata" ] , `${ PROTOCOL }://${ HOST }:${ PORT }/${ EMULATOR_ACCOUNT_NAME }/$metadata#Tables/@Element` ) ; assert . deepStrictEqual ( bodies [ "odata.id" ] , `${ PROTOCOL }://${ HOST }:${ PORT }/${ EMULATOR_ACCOUNT_NAME }/Tables(${ tableName })` ) ; assert . deepStrictEqual ( bodies [ "odata.editLink" ] , `Tables(${ tableName })` ) ; } done ( ) ; } ) ; } ) ; it ( "createTable, prefer=return-content, accept=application/json;odata=minimalmetadata @loki" , ( done ) => { done ( ) ; } ) ; it ( "createTable, prefer=return-content, accept=application/json;odata=nometadata @loki" , ( done ) => { done ( ) ; } ) ; it ( "queryTable, accept=application/json;odata=fullmetadata @loki" , ( done ) => { requestOverride . headers = { accept : "application/json;odata=fullmetadata" } ; tableService . listTablesSegmented ( null as any , { maxResults : 20 } , ( error , result , response ) => { assert . deepStrictEqual ( error , null ) ; assert . strictEqual ( response . statusCode , 200 ) ; const headers = response . headers ! ; assert . strictEqual ( headers [ "x-ms-version" ] , TABLE_API_VERSION ) ; const bodies = response . body ! as any ; assert . deepStrictEqual ( bodies [ "odata.metadata" ] , `${ PROTOCOL }://${ HOST }:${ PORT }/${ EMULATOR_ACCOUNT_NAME }/$metadata#Tables` ) ; assert . ok ( bodies . value [ 0 ] . TableName ) ; assert . ok ( bodies . value [ 0 ] [ "odata.type" ] ) ; assert . ok ( bodies . value [ 0 ] [ "odata.id" ] ) ; assert . ok ( bodies . value [ 0 ] [ "odata.editLink" ] ) ; done ( ) ; } ) ; } ) ; it ( "queryTable, accept=application/json;odata=minimalmetadata @loki" , ( done ) => { requestOverride . headers = { accept : "application/json;odata=minimalmetadata" } ; tableService . listTablesSegmented ( null as any , ( error , result , response ) => { if ( ! error ) { assert . strictEqual ( response . statusCode , 200 ) ; const headers = response . headers ! ; assert . strictEqual ( headers [ "x-ms-version" ] , TABLE_API_VERSION ) ; const bodies = response . body ! as any ; assert . deepStrictEqual ( bodies [ "odata.metadata" ] , `${ PROTOCOL }://${ HOST }:${ PORT }/${ EMULATOR_ACCOUNT_NAME }/$metadata#Tables` ) ; assert . ok ( bodies . value [ 0 ] . TableName ) ; } done ( ) ; } ) ; } ) ; it ( "queryTable, accept=application/json;odata=nometadata @loki" , ( done ) => { requestOverride . headers = { accept : "application/json;odata=nometadata" } ; tableService . listTablesSegmented ( null as any , ( error , result , response ) => { if ( ! error ) { assert . strictEqual ( response . statusCode , 200 ) ; const headers = response . headers ! ; assert . strictEqual ( headers [ "x-ms-version" ] , TABLE_API_VERSION ) ; const bodies = response . body ! as any ; assert . ok ( bodies . value [ 0 ] . TableName ) ; } done ( ) ; } ) ; } ) ; it ( "deleteTable that exists, @loki" , ( done ) => { requestOverride . headers = { } ; const tableToDelete = getUniqueName ( "table" ) + "del" ; tableService . createTable ( tableToDelete , ( error , result , response ) => { if ( ! error ) { tableService . deleteTable ( tableToDelete , ( deleteError , deleteResult ) => { if ( ! deleteError ) { assert . strictEqual ( deleteResult . statusCode , 204 ) ; } else { assert . ifError ( deleteError ) ; } done ( ) ; } ) ; } else { assert . fail ( "Test failed to create the table" ) ; done ( ) ; } } ) ; } ) ; it ( "deleteTable that does not exist, @loki" , ( done ) => { requestOverride . headers = { } ; const tableToDelete = tableName + "causeerror" ; tableService . deleteTable ( tableToDelete , ( error , result ) => { assert . strictEqual ( result . statusCode , 404 ) ; const storageError = error as any ; assert . strictEqual ( storageError . code , "ResourceNotFound" ) ; done ( ) ; } ) ; } ) ; it ( "createTable with invalid version, @loki" , ( done ) => { requestOverride . headers = { [ HeaderConstants . X_MS_VERSION ] : "invalid" } ; tableService . createTable ( "test" , ( error , result ) => { assert . strictEqual ( result . statusCode , 400 ) ; done ( ) ; } ) ; } ) ; it ( "Should have a valid OData Metadata value when inserting a table, @loki" , ( done ) => { requestOverride . headers = { Prefer : "return-content" , accept : "application/json;odata=fullmetadata" } ; const newTableName = getUniqueName ( "table" ) ; tableService . createTable ( newTableName , ( error , result , response ) => { if ( ! error && result !== undefined && response !== undefined && response . body !== undefined ) { const body = response . body as object ; const meta = body [ "odata.metadata" as keyof object ] ; assert . strictEqual ( meta . endsWith ( "/@Element" ) , true ) ; done ( ) ; } else { assert . ifError ( error ) ; done ( ) ; } } ) ; } ) ; it ( "SetAccessPolicy should work @loki" , ( done ) => { const tableAcl = { "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" : { Permissions : "raud" , Expiry : new Date ( "2018-12-31T11:22:33.4567890Z" ) , Start : new Date ( "2017-12-31T11:22:33.4567890Z" ) } , "policy2" : { Permissions : "a" , Expiry : new Date ( "2030-11-31T11:22:33.4567890Z" ) , Start : new Date ( "2017-12-31T11:22:33.4567890Z" ) } } ; tableService . createTable ( tableName + "setACL" , ( error ) => { if ( error ) { assert . ifError ( error ) ; } const setClientRequestId = "b86e2b01-a7b5-4df2-b190-205a0c24bd36" ; tableService . setTableAcl ( tableName + "setACL" , tableAcl , { clientRequestId : setClientRequestId } , ( error , result , response ) => { if ( error ) { assert . ifError ( error ) ; } if ( response . headers ) { assert . strictEqual ( response . headers [ "x-ms-client-request-id" ] , setClientRequestId ) ; } tableService . getTableAcl ( tableName + "setACL" , { clientRequestId : setClientRequestId } , ( error , result , response ) => { if ( error ) { assert . ifError ( error ) ; } if ( response . headers ) { assert . strictEqual ( response . headers [ "x-ms-client-request-id" ] , setClientRequestId ) ; } assert . deepStrictEqual ( result . signedIdentifiers , tableAcl ) ; done ( ) ; } ) ; } ) ; } ) ; } ) ; it ( "setAccessPolicy negative @loki" , ( done ) => { const tableAcl = { "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" : { Permissions : "rwdl" , Expiry : new Date ( "2018-12-31T11:22:33.4567890Z" ) , Start : new Date ( "2017-12-31T11:22:33.4567890Z" ) } , "policy2" : { Permissions : "a" , Expiry : new Date ( "2030-11-31T11:22:33.4567890Z" ) , Start : new Date ( "2017-12-31T11:22:33.4567890Z" ) } } ; tableService . createTable ( tableName + "setACLNeg" , ( error ) => { if ( error ) { assert . ifError ( error ) ; } tableService . setTableAcl ( tableName + "setACLNeg" , tableAcl , ( error ) => { assert . ok ( error ) ; done ( ) ; } ) ; } ) ; } ) ; } ) ;