Skip to content

Commit

Permalink
cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 22, 2024
1 parent 30ead31 commit 1502ed3
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,9 @@ test('it should log errors to console if the option is set', async () => {

test('it should handle calls against the ic-management canister that are rejected', async () => {
const identity = new AnonymousIdentity();
identity.getPrincipal().toString(); //?
identity.getPrincipal().toString();

// Response generated by calling a locally deployed replica of the management canister
// Response generated by calling a locally deployed replica of the management canister, cloned using fetchCloner
const mockResponse = {
headers: [
['access-control-allow-origin', '*'],
Expand All @@ -839,6 +839,7 @@ test('it should handle calls against the ic-management canister that are rejecte
now: 1729553760128,
};

// Mock the fetch implementation, resolving a pre-calculated response
const mockFetch: jest.Mock = jest.fn(() => {
return Promise.resolve({
...mockResponse,
Expand All @@ -847,10 +848,8 @@ test('it should handle calls against the ic-management canister that are rejecte
});
});

jest.useFakeTimers({
legacyFakeTimers: false,
});

// Mock time so certificates can be accurately decoded
jest.useFakeTimers();
jest.setSystemTime(mockResponse.now);

const agent = await HttpAgent.createSync({
Expand All @@ -859,8 +858,10 @@ test('it should handle calls against the ic-management canister that are rejecte
host: 'http://localhost:4943',
});

// Use management canister call
const management = getManagementCanister({ agent });

// Call snapshot was made when the test canister was not authorized to be called by the anonymous identity. It should reject
expect(
management.canister_status({
canister_id: Principal.from('bkyz2-fmaaa-aaaaa-qaaaq-cai'),
Expand All @@ -871,8 +872,8 @@ test('it should handle calls against the ic-management canister that are rejecte
});
test('it should handle calls against the ic-management canister that succeed', async () => {
const identity = new AnonymousIdentity();
jest.createMockFromModule('./types');

// Response generated by calling a locally deployed replica of the management canister, cloned using fetchCloner
const mockResponse = {
headers: [
['access-control-allow-origin', '*'],
Expand All @@ -887,9 +888,7 @@ test('it should handle calls against the ic-management canister that succeed', a
now: 1729635546372,
};

jest.useFakeTimers();
jest.setSystemTime(mockResponse.now);

// Mock the fetch implementation, resolving a pre-calculated response
const mockFetch: jest.Mock = jest.fn(() => {
return Promise.resolve({
...mockResponse,
Expand All @@ -898,6 +897,11 @@ test('it should handle calls against the ic-management canister that succeed', a
});
});

// Mock time so certificates can be accurately decoded
jest.useFakeTimers();
jest.setSystemTime(mockResponse.now);

// Pass in rootKey from replica (used because test was written using local replica)
const agent = await HttpAgent.createSync({
identity,
fetch: mockFetch,
Expand All @@ -907,6 +911,7 @@ test('it should handle calls against the ic-management canister that succeed', a
),
});

// Use management canister call
const management = getManagementCanister({ agent });

// Important - override nonce when making request to ensure reproducible result
Expand All @@ -915,6 +920,7 @@ test('it should handle calls against the ic-management canister that succeed', a
return args;
});

// Call snapshot was made after the test canister was authorized to be called by the anonymous identity. It should resolve the status
const status = await management.canister_status({
canister_id: Principal.from('bkyz2-fmaaa-aaaaa-qaaaq-cai'),
});
Expand Down

0 comments on commit 1502ed3

Please sign in to comment.