| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | // Copyright 2022 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { assert } from 'chai'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import * as durations from '../../util/durations'; | 
					
						
							|  |  |  | import type { App, Bootstrap } from './fixtures'; | 
					
						
							|  |  |  | import { initStorage, debug } from './fixtures'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | describe('storage service', function (this: Mocha.Suite) { | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |   this.timeout(durations.MINUTE); | 
					
						
							| 
									
										
										
										
											2023-09-19 19:02:37 +02:00
										 |  |  |   this.retries(4); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   let bootstrap: Bootstrap; | 
					
						
							|  |  |  |   let app: App; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(async () => { | 
					
						
							|  |  |  |     ({ bootstrap, app } = await initStorage()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  |   afterEach(async function (this: Mocha.Context) { | 
					
						
							| 
									
										
										
										
											2022-07-08 13:46:25 -07:00
										 |  |  |     if (!bootstrap) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 09:53:20 -07:00
										 |  |  |     await bootstrap.maybeSaveLogs(this.currentTest, app); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     await app.close(); | 
					
						
							|  |  |  |     await bootstrap.teardown(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should handle message request state changes', async () => { | 
					
						
							|  |  |  |     const { phone, desktop, server } = bootstrap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |     const initialState = await phone.expectStorageState('initial state'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     debug('Creating stranger'); | 
					
						
							|  |  |  |     const stranger = await server.createPrimaryDevice({ | 
					
						
							|  |  |  |       profileName: 'Mysterious Stranger', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ourKey = await desktop.popSingleUseKey(); | 
					
						
							|  |  |  |     await stranger.addSingleUseKey(desktop, ourKey); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('Sending a message from a stranger'); | 
					
						
							|  |  |  |     await stranger.sendText(desktop, 'Hello!', { | 
					
						
							|  |  |  |       withProfileKey: true, | 
					
						
							|  |  |  |       timestamp: bootstrap.getTimestamp(), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const window = await app.getWindow(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-26 18:23:32 -04:00
										 |  |  |     const leftPane = window.locator('#LeftPane'); | 
					
						
							|  |  |  |     const conversationStack = window.locator('.Inbox__conversation-stack'); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     debug('Opening conversation with a stranger'); | 
					
						
							| 
									
										
										
										
											2023-08-16 22:54:39 +02:00
										 |  |  |     debug(stranger.toContact().aci); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     await leftPane | 
					
						
							| 
									
										
										
										
											2023-08-16 22:54:39 +02:00
										 |  |  |       .locator(`[data-testid="${stranger.toContact().aci}"]`) | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |       .click(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |     debug("Verify that we stored stranger's profile key"); | 
					
						
							|  |  |  |     const postMessageState = await phone.waitForStorageState({ | 
					
						
							|  |  |  |       after: initialState, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       assert.strictEqual(postMessageState.version, 2); | 
					
						
							|  |  |  |       assert.isFalse(postMessageState.getContact(stranger)?.whitelisted); | 
					
						
							|  |  |  |       assert.strictEqual( | 
					
						
							|  |  |  |         postMessageState.getContact(stranger)?.profileKey?.length, | 
					
						
							|  |  |  |         32 | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // ContactRecord
 | 
					
						
							|  |  |  |       const { added, removed } = postMessageState.diff(initialState); | 
					
						
							|  |  |  |       assert.strictEqual(added.length, 1, 'only one record must be added'); | 
					
						
							|  |  |  |       assert.strictEqual(removed.length, 0, 'no records should be removed'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     debug('Accept conversation from a stranger'); | 
					
						
							|  |  |  |     await conversationStack | 
					
						
							|  |  |  |       .locator('.module-message-request-actions button >> "Accept"') | 
					
						
							|  |  |  |       .click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('Verify that storage state was updated'); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const nextState = await phone.waitForStorageState({ | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |         after: postMessageState, | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |       assert.strictEqual(nextState.version, 3); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |       assert.isTrue(nextState.getContact(stranger)?.whitelisted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // ContactRecord
 | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |       const { added, removed } = nextState.diff(postMessageState); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |       assert.strictEqual(added.length, 1, 'only one record must be added'); | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |       assert.strictEqual( | 
					
						
							|  |  |  |         removed.length, | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         'only one record should be removed' | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Stranger should receive our profile key
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const { body, source, dataMessage } = await stranger.waitForMessage(); | 
					
						
							|  |  |  |       assert.strictEqual(body, '', 'profile key message has no body'); | 
					
						
							|  |  |  |       assert.strictEqual( | 
					
						
							|  |  |  |         source, | 
					
						
							|  |  |  |         desktop, | 
					
						
							|  |  |  |         'profile key message has valid source' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       assert.isTrue( | 
					
						
							|  |  |  |         phone.profileKey | 
					
						
							|  |  |  |           .serialize() | 
					
						
							|  |  |  |           .equals(dataMessage.profileKey ?? new Uint8Array(0)), | 
					
						
							|  |  |  |         'profile key message has correct profile key' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('Enter message text'); | 
					
						
							|  |  |  |     const composeArea = window.locator( | 
					
						
							| 
									
										
										
										
											2023-07-26 18:23:32 -04:00
										 |  |  |       '.composition-area-wrapper, .Inbox__conversation .ConversationView' | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-01-12 19:24:59 -05:00
										 |  |  |     const input = composeArea.locator('[data-testid=CompositionInput]'); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await input.type('hello stranger!'); | 
					
						
							|  |  |  |     await input.press('Enter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const { body, source } = await stranger.waitForMessage(); | 
					
						
							|  |  |  |       assert.strictEqual(body, 'hello stranger!', 'text message has body'); | 
					
						
							|  |  |  |       assert.strictEqual(source, desktop, 'text message has valid source'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('Verifying the final manifest version'); | 
					
						
							|  |  |  |     const finalState = await phone.expectStorageState('consistency check'); | 
					
						
							| 
									
										
										
										
											2022-03-02 14:53:47 -08:00
										 |  |  |     assert.strictEqual(finalState.version, 3); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |