-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
191 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { fakeUrsulas } from '@nucypher/test-utils'; | ||
import axios, { HttpStatusCode } from 'axios'; | ||
import { SpyInstance, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
import { | ||
GetUrsulasResult, | ||
PorterClient, | ||
Ursula, | ||
initialize, | ||
toHexString, | ||
} from '../src'; | ||
|
||
const fakePorterUris = [ | ||
'https://_this_should_crash.com/', | ||
'https://2_this_should_crash.com/', | ||
'https://_this_should_work.com/', | ||
]; | ||
|
||
const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => { | ||
const fakePorterUrsulas = ( | ||
mockUrsulas: readonly Ursula[], | ||
): GetUrsulasResult => { | ||
return { | ||
result: { | ||
ursulas: mockUrsulas.map(({ encryptingKey, uri, checksumAddress }) => ({ | ||
encrypting_key: toHexString(encryptingKey.toCompressedBytes()), | ||
uri: uri, | ||
checksum_address: checksumAddress, | ||
})), | ||
}, | ||
version: '5.2.0', | ||
}; | ||
}; | ||
|
||
return vi.spyOn(axios, 'request').mockImplementation(async (config) => { | ||
switch (config.baseURL) { | ||
case fakePorterUris[2]: | ||
return Promise.resolve({ | ||
status: HttpStatusCode.Ok, | ||
data: fakePorterUrsulas(ursulas), | ||
}); | ||
case fakePorterUris[0]: | ||
throw new Error(); | ||
default: | ||
throw Promise.resolve({ status: HttpStatusCode.BadRequest }); | ||
} | ||
}); | ||
}; | ||
|
||
describe('PorterClient', () => { | ||
beforeAll(async () => { | ||
await initialize(); | ||
}); | ||
|
||
it('should work when at least one ursula URI is valid', async () => { | ||
const ursulas = fakeUrsulas(); | ||
const getUrsulasSpy = mockGetUrsulas(ursulas); | ||
const porterClient = new PorterClient(fakePorterUris); | ||
const result = await porterClient.getUrsulas(ursulas.length); | ||
|
||
expect( | ||
result.every((u: Ursula, index: number) => { | ||
const expectedUrsula = ursulas[index]; | ||
return ( | ||
u.checksumAddress === expectedUrsula.checksumAddress && | ||
u.uri === expectedUrsula.uri && | ||
u.encryptingKey.equals(expectedUrsula.encryptingKey) | ||
); | ||
}), | ||
).toBeTruthy(); | ||
const params = { | ||
method: 'get', | ||
url: '/get_ursulas', | ||
params: { | ||
exclude_ursulas: [], | ||
include_ursulas: [], | ||
quantity: ursulas.length, | ||
}, | ||
}; | ||
|
||
expect(getUrsulasSpy).toBeCalledTimes(fakePorterUris.length); | ||
fakePorterUris.forEach((value, index) => { | ||
expect(getUrsulasSpy).toHaveBeenNthCalledWith( | ||
index + 1, | ||
expect.objectContaining({ ...params, baseURL: value }), | ||
); | ||
}); | ||
}); | ||
|
||
it('returns error in case all porters fail', async () => { | ||
const ursulas = fakeUrsulas(); | ||
mockGetUrsulas(ursulas); | ||
let porterClient = new PorterClient([fakePorterUris[0], fakePorterUris[1]]); | ||
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); | ||
porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]); | ||
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ export { | |
Domain, | ||
domains, | ||
fromBytes, | ||
getPorterUri, | ||
getPorterUris, | ||
initialize, | ||
toBytes, | ||
toHexString, | ||
|
Oops, something went wrong.