-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: unit tests for events createdbyuserid migration code
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/lib/features/events/event-created-by-migration.test.ts
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,116 @@ | ||
import EventStore from './event-store'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
import dbInit, { ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
|
||
let db: ITestDb; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('events_test', getLogger); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.rawDatabase('events').del(); | ||
await db.rawDatabase('users').del(); | ||
await db.destroy(); | ||
}); | ||
|
||
test('sets created_by_user_id on events with user username/email set as created_by', async () => { | ||
const store = new EventStore(db.rawDatabase, getLogger); | ||
|
||
await db.rawDatabase('users').insert({ username: 'test1' }); | ||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test1', | ||
feature_name: `feature1`, | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await store.setCreatedByUserId(200); | ||
|
||
const user = await db | ||
.rawDatabase('users') | ||
.where({ username: 'test1' }) | ||
.first('id'); | ||
|
||
const events = await db.rawDatabase('events').select('*'); | ||
const notSet = events.filter( | ||
(e) => !e.created_by_user_id && e.data.test === 'data-migrate', | ||
); | ||
const test1 = events.filter( | ||
(e) => | ||
e.created_by_user_id === user.id && e.data.test === 'data-migrate', | ||
); | ||
expect(notSet).toHaveLength(0); | ||
expect(test1).toHaveLength(1); | ||
}); | ||
|
||
test('sets created_by_user_id on a mix of events and created_bys', async () => { | ||
const store = new EventStore(db.rawDatabase, getLogger); | ||
|
||
await db.rawDatabase('users').insert({ username: 'test2' }); | ||
|
||
await db.rawDatabase('api_tokens').insert({ | ||
secret: 'token1', | ||
username: 'adm-token', | ||
type: 'admin', | ||
environment: 'default', | ||
token_name: 'admin-token', | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test2', | ||
feature_name: `feature1`, | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'strategy-created', | ||
created_by: 'migration', | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'api-token-created', | ||
created_by: 'init-api-tokens', | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'application-created', | ||
created_by: '::1', | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'unknown', | ||
feature_name: `feature2`, | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'adm-token', | ||
feature_name: `feature3`, | ||
data: `{"test": "data-migrate"}`, | ||
}); | ||
|
||
await store.setCreatedByUserId(200); | ||
|
||
const user = await db | ||
.rawDatabase('users') | ||
.where({ username: 'test2' }) | ||
.first('id'); | ||
|
||
const events = await db.rawDatabase('events').select('*'); | ||
const notSet = events.filter( | ||
(e) => !e.created_by_user_id && e.data.test === 'data-migrate', | ||
); | ||
const test = events.filter( | ||
(e) => | ||
e.created_by_user_id === user.id && e.data.test === 'data-migrate', | ||
); | ||
expect(notSet).toHaveLength(1); | ||
expect(test).toHaveLength(1); | ||
}); |