-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow tenant_id and user_id context to be set
- Loading branch information
Showing
16 changed files
with
334 additions
and
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Nile from './Server'; | ||
|
||
describe('server', () => { | ||
it('has reasonable defaults', () => { | ||
const config = { | ||
database: 'database', | ||
workspace: 'workspace', | ||
}; | ||
const server = Nile(config); | ||
expect(server.config.db.connection).toEqual({ | ||
host: 'db.thenile.dev', | ||
port: 5432, | ||
database: 'database', | ||
}); | ||
expect(server.config.api.basePath).toEqual('https://api.thenile.dev'); | ||
}); | ||
it('sets a tenant id everywhere when set', async () => { | ||
const config = { | ||
database: 'database', | ||
workspace: 'workspace', | ||
}; | ||
const nile = Nile(config); | ||
nile.tenantId = 'tenantId'; | ||
nile.userId = 'userId'; | ||
for (const api in nile.api) { | ||
// @ts-expect-error - checking api | ||
const _api = nile.api[api]; | ||
expect(_api.tenantId).toEqual('tenantId'); | ||
} | ||
|
||
// @ts-expect-error - checking db | ||
expect(nile._db.tenantId).toEqual('tenantId'); | ||
// @ts-expect-error - checking db | ||
expect(nile._db.userId).toEqual('userId'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,70 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Knex } from 'knex'; | ||
|
||
import NileDB, { handleWithUser, handleWithTenant } from './index'; | ||
import NileDB from './index'; | ||
|
||
const properties = ['knex', 'tenantId', 'userId', 'db', 'config']; | ||
describe('db', () => { | ||
it('is actually knex', () => { | ||
const db = NileDB({ client: 'pg' }); | ||
expect(String(db)).toContain('knex'); | ||
}); | ||
|
||
it('sets and resets tenant', async () => { | ||
const context = { | ||
client: { | ||
raw: jest.fn(), | ||
}, | ||
}; | ||
await handleWithTenant( | ||
context as unknown as Knex.QueryBuilder<any, any>, | ||
'tenantId' | ||
); | ||
expect(context.client.raw).toBeCalledTimes(2); | ||
expect(context.client.raw).toBeCalledWith('RESET nile.tenant_id'); | ||
expect(context.client.raw).toBeCalledWith( | ||
"SET nile.tenant_id = 'tenantId'" | ||
); | ||
}); | ||
it('resets tenant with a blank value', async () => { | ||
const context = { | ||
client: { | ||
raw: jest.fn(), | ||
}, | ||
}; | ||
await handleWithTenant( | ||
context as unknown as Knex.QueryBuilder<any, any>, | ||
null | ||
); | ||
expect(context.client.raw).toBeCalledTimes(1); | ||
expect(context.client.raw).toBeCalledWith('RESET nile.tenant_id'); | ||
}); | ||
it('sets and resets user', async () => { | ||
const context = { | ||
client: { | ||
raw: jest.fn(), | ||
it('has expected properties', () => { | ||
const db = new NileDB({ | ||
workspace: 'workspace', | ||
database: 'database', | ||
db: { | ||
connection: { port: 4433 }, | ||
}, | ||
}; | ||
await handleWithUser( | ||
context as unknown as Knex.QueryBuilder<any, any>, | ||
'tenantId', | ||
'userId' | ||
); | ||
expect(context.client.raw).toBeCalledTimes(4); | ||
expect(context.client.raw).toBeCalledWith('RESET nile.user_id'); | ||
expect(context.client.raw).toBeCalledWith("SET nile.user_id = 'userId'"); | ||
}); | ||
it('resets user with a blank value', async () => { | ||
const context = { | ||
client: { | ||
raw: jest.fn(), | ||
api: { | ||
token: 'blah', | ||
}, | ||
}; | ||
await handleWithUser( | ||
context as unknown as Knex.QueryBuilder<any, any>, | ||
undefined, | ||
undefined | ||
); | ||
expect(context.client.raw).toBeCalledTimes(2); | ||
expect(context.client.raw).toBeCalledWith('RESET nile.user_id'); | ||
tenantId: null, | ||
userId: null, | ||
}); | ||
expect(Object.keys(db).sort()).toEqual(properties.sort()); | ||
}); | ||
}); |
Oops, something went wrong.