|
1 | 1 | import { privateKeyToAccount } from 'viem/accounts'; |
2 | 2 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
3 | 3 | import { encryptIdentity } from '../../src/identity/identity-encryption'; |
4 | | -import { getSessionNonce, loginWithWallet, prepareSiweMessage, restoreKeys, signup } from '../../src/identity/login'; |
| 4 | +import { |
| 5 | + getSessionNonce, |
| 6 | + loginWithKeys, |
| 7 | + loginWithWallet, |
| 8 | + prepareSiweMessage, |
| 9 | + restoreKeys, |
| 10 | + signup, |
| 11 | +} from '../../src/identity/login'; |
5 | 12 | import type { IdentityKeys, Signer, Storage } from '../../src/identity/types'; |
6 | 13 | import type * as Messages from '../../src/messages'; |
7 | 14 |
|
@@ -446,3 +453,126 @@ describe('loginWithWallet', () => { |
446 | 453 | expect(result.keys).toEqual(identityKeys); |
447 | 454 | }); |
448 | 455 | }); |
| 456 | + |
| 457 | +describe('loginWithKeys', () => { |
| 458 | + let mockStorage: Storage; |
| 459 | + const accountId = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'; |
| 460 | + const identityKeys: IdentityKeys = { |
| 461 | + signaturePublicKey: '0x0262701b2eb1b6b37ad03e24445dfcad1b91309199e43017b657ce2604417c12f5', |
| 462 | + signaturePrivateKey: '0x88bb6f20de8dc1787c722dc847f4cf3d00285b8955445f23c483d1237fe85366', |
| 463 | + encryptionPrivateKey: '0xbbf164a93b0f78a85346017fa2673cf367c64d81b1c3d6af7ad45e308107a812', |
| 464 | + encryptionPublicKey: '0x595e1a6b0bb346d83bc382998943d2e6d9210fd341bc8b9f41a7229eede27240', |
| 465 | + }; |
| 466 | + const location = { host: 'localhost', origin: 'http://localhost' }; |
| 467 | + |
| 468 | + beforeEach(() => { |
| 469 | + mockStorage = { |
| 470 | + getItem: vi.fn(), |
| 471 | + setItem: vi.fn(), |
| 472 | + removeItem: vi.fn(), |
| 473 | + }; |
| 474 | + }); |
| 475 | + |
| 476 | + afterEach(() => { |
| 477 | + vi.restoreAllMocks(); |
| 478 | + }); |
| 479 | + |
| 480 | + it('should successfully login with keys and validate the valid session token', async () => { |
| 481 | + const sessionToken = 'test-session-token'; |
| 482 | + |
| 483 | + // @ts-expect-error |
| 484 | + mockStorage.getItem.mockImplementation((key) => { |
| 485 | + if (key === `hypergraph:dev:session-token:${accountId}`) { |
| 486 | + return sessionToken; |
| 487 | + } |
| 488 | + |
| 489 | + return null; |
| 490 | + }); |
| 491 | + |
| 492 | + vi.spyOn(global, 'fetch').mockImplementation((url) => { |
| 493 | + if (url.toString() === 'http://localhost:3000/whoami') { |
| 494 | + return Promise.resolve({ |
| 495 | + status: 200, |
| 496 | + text: () => Promise.resolve(accountId), |
| 497 | + } as Response); |
| 498 | + } |
| 499 | + |
| 500 | + return vi.fn() as never; |
| 501 | + }); |
| 502 | + |
| 503 | + const result = await loginWithKeys(identityKeys, accountId, 'http://localhost:3000', 1, mockStorage, location); |
| 504 | + |
| 505 | + expect(result.accountId).toBe(accountId); |
| 506 | + expect(result.sessionToken).toBe(sessionToken); |
| 507 | + expect(result.keys).toEqual(identityKeys); |
| 508 | + }); |
| 509 | + |
| 510 | + it('should successfully login with keys and retrieve a new session token', async () => { |
| 511 | + const sessionToken = 'test-session-token'; |
| 512 | + |
| 513 | + vi.spyOn(global, 'fetch').mockImplementation((url) => { |
| 514 | + if (url.toString() === 'http://localhost:3000/login/with-signing-key') { |
| 515 | + return Promise.resolve({ |
| 516 | + status: 200, |
| 517 | + json: () => Promise.resolve({ sessionToken }), |
| 518 | + } as Response); |
| 519 | + } |
| 520 | + if (url.toString() === 'http://localhost:3000/login/nonce') { |
| 521 | + return Promise.resolve({ |
| 522 | + status: 200, |
| 523 | + json: () => Promise.resolve({ sessionNonce: 'Sv2dJppgx9SKDGCIb' }), |
| 524 | + } as Response); |
| 525 | + } |
| 526 | + |
| 527 | + return vi.fn() as never; |
| 528 | + }); |
| 529 | + |
| 530 | + const result = await loginWithKeys(identityKeys, accountId, 'http://localhost:3000', 1, mockStorage, location); |
| 531 | + |
| 532 | + expect(result.accountId).toBe(accountId); |
| 533 | + expect(result.sessionToken).toBe(sessionToken); |
| 534 | + expect(result.keys).toEqual(identityKeys); |
| 535 | + }); |
| 536 | + |
| 537 | + it('should get new session token if existing token is invalid', async () => { |
| 538 | + const sessionToken = 'test-session-token'; |
| 539 | + |
| 540 | + // @ts-expect-error |
| 541 | + mockStorage.getItem.mockImplementation((key) => { |
| 542 | + if (key === `hypergraph:dev:session-token:${accountId}`) { |
| 543 | + return sessionToken; |
| 544 | + } |
| 545 | + |
| 546 | + return null; |
| 547 | + }); |
| 548 | + |
| 549 | + vi.spyOn(global, 'fetch').mockImplementation((url) => { |
| 550 | + if (url.toString() === 'http://localhost:3000/whoami') { |
| 551 | + return Promise.resolve({ |
| 552 | + status: 200, |
| 553 | + text: () => Promise.resolve(accountId), |
| 554 | + } as Response); |
| 555 | + } |
| 556 | + if (url.toString() === 'http://localhost:3000/login/with-signing-key') { |
| 557 | + return Promise.resolve({ |
| 558 | + status: 200, |
| 559 | + json: () => Promise.resolve({ sessionToken }), |
| 560 | + } as Response); |
| 561 | + } |
| 562 | + if (url.toString() === 'http://localhost:3000/login/nonce') { |
| 563 | + return Promise.resolve({ |
| 564 | + status: 200, |
| 565 | + json: () => Promise.resolve({ sessionNonce: 'Sv2dJppgx9SKDGCIb' }), |
| 566 | + } as Response); |
| 567 | + } |
| 568 | + |
| 569 | + return vi.fn() as never; |
| 570 | + }); |
| 571 | + |
| 572 | + const result = await loginWithKeys(identityKeys, accountId, 'http://localhost:3000', 1, mockStorage, location); |
| 573 | + |
| 574 | + expect(result.accountId).toBe(accountId); |
| 575 | + expect(result.sessionToken).toBe(sessionToken); |
| 576 | + expect(result.keys).toEqual(identityKeys); |
| 577 | + }); |
| 578 | +}); |
0 commit comments