Skip to content

Commit

Permalink
fix!: client should not throw on 404 (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Dec 6, 2021
1 parent 84f55e1 commit a4cce7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/api/test/mocks/pgrest/get_user.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
[{
"id": 1,
"issuer": "issuer-str",
"keys": [
Expand All @@ -8,4 +8,4 @@
"secret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwiaXNzIjoid2ViMy1zdG9yYWdlIiwiaWF0IjoxNjMzOTU3Mzg5ODcyLCJuYW1lIjoidGVzdCJ9.KEH0jHUfJls44YWsj8uex_zj0dUIvdyqGalv2rhWnx8"
}
]
}
}]
27 changes: 15 additions & 12 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export class DBClient {
* Get user by its issuer.
*
* @param {string} issuer
* @return {Promise<import('./db-client-types').UserOutput>}
* @return {Promise<import('./db-client-types').UserOutput | undefined>}
*/
async getUser (issuer) {
/** @type {{ data: import('./db-client-types').UserOutput, error: PostgrestError }} */
/** @type {{ data: import('./db-client-types').UserOutput[], error: PostgrestError }} */
const { data, error } = await this._client
.from('user')
.select(`
Expand All @@ -87,13 +87,12 @@ export class DBClient {
updated:updated_at
`)
.eq('issuer', issuer)
.single()

if (error) {
throw new DBError(error)
}

return data
return data.length ? data[0] : undefined
}

/**
Expand Down Expand Up @@ -614,7 +613,7 @@ export class DBClient {
*
* @param {string} issuer
* @param {string} secret
* @return {Promise<import('./db-client-types').AuthKey>}
* @return {Promise<import('./db-client-types').AuthKey | undefined>}
*/
async getKey (issuer, secret) {
/** @type {{ data, error: PostgrestError } */
Expand All @@ -630,22 +629,26 @@ export class DBClient {
})
.filter('keys.deleted_at', 'is', null)
.eq('keys.secret', secret)
.single()

if (error) {
throw new DBError(error)
}

if (!data.keys.length) {
throw new Error('user has no key with given secret')
if (!data.length) {
return undefined
}

const keyData = data[0]
if (!keyData.keys.length) {
return undefined
}

return {
_id: data.keys[0]._id,
name: data.keys[0].name,
_id: keyData.keys[0]._id,
name: keyData.keys[0].name,
user: {
_id: data._id,
issuer: data.issuer
_id: keyData._id,
issuer: keyData.issuer
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions packages/db/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@ describe('user operations', () => {
assert.strictEqual(user.publicAddress, publicAddress, 'user has correct public address')
})

it('should fail to get a non existing user', async () => {
try {
await client.getUser('fake-issuer')
} catch (err) {
assert(err, 'errored to get a non existing user')
return
}
throw new Error('should fail to get a non existing user')
it('should return undefined to get a non existing user', async () => {
const user = await client.getUser('fake-issuer')
assert.strictEqual(user, undefined)
})

it('should update user with same issuer (login)', async () => {
Expand Down Expand Up @@ -88,6 +83,13 @@ describe('user operations', () => {
assert.strictEqual(updatedUser.created, user.created, 'user has same created timestamp')
})

it('should return undefined to get non existing key for user', async () => {
const secret = 'test-secret-fail'
const fetchedKey = await client.getKey(user.issuer, secret)

assert.strictEqual(fetchedKey, undefined)
})

it('can create auth keys for user', async () => {
const name = 'test-key-name'
const secret = 'test-secret'
Expand Down

0 comments on commit a4cce7e

Please sign in to comment.