Skip to content

Commit

Permalink
chore: fix tags in api user info (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamalton authored May 31, 2022
1 parent 51abb35 commit fdf6c76
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/api/test/scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JWT_ISSUER } from '../../src/constants.js'
import { SALT } from './worker-globals.js'
import { sha256 } from 'multiformats/hashes/sha2'
import * as pb from '@ipld/dag-pb'
import { DBClient } from '@web3-storage/db'

const libp2pKeyCode = 0x72
const lifetime = 1000 * 60 * 60
Expand Down Expand Up @@ -54,7 +55,6 @@ export async function updateNameRecord (privKey, existingRecord, newValue) {
export function getTestJWT (sub = 'test-magic-issuer', name = 'test-magic-issuer') {
return JWT.sign({ sub, iss: JWT_ISSUER, iat: 1633957389872, name }, SALT)
}

/**
* @param {number} code
* @returns {Promise<string>}
Expand All @@ -63,3 +63,18 @@ export async function randomCid (code = pb.code) {
const hash = await sha256.digest(Buffer.from(`${Math.random()}`))
return CID.create(1, code, hash).toString()
}

/**
* Create a new DB client instance from the current environment variables.
*/
export function getDBClient () {
const token = process.env.PG_REST_JWT
const endpoint = process.env.PG_REST_URL
if (!token) {
throw new Error('missing PG_REST_JWT environment var')
}
if (!endpoint) {
throw new Error('missing PG_REST_URL environment var')
}
return new DBClient({ token, endpoint, postgres: true })
}
44 changes: 43 additions & 1 deletion packages/api/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import assert from 'assert'
import fetch from '@web-std/fetch'
import { endpoint } from './scripts/constants.js'
import { getTestJWT } from './scripts/helpers.js'
import { getTestJWT, getDBClient } from './scripts/helpers.js'
import userUploads from './fixtures/pgrest/get-user-uploads.js'

describe('GET /user/account', () => {
Expand Down Expand Up @@ -33,6 +33,48 @@ describe('GET /user/account', () => {
})
})

describe('GET /user/info', () => {
it('error if not authenticated with magic.link', async () => {
const token = await getTestJWT()
const res = await fetch(new URL('user/account', endpoint), {
headers: { Authorization: `Bearer ${token}` }
})
assert(!res.ok)
assert.strictEqual(res.status, 401)
})

it('error if no auth header', async () => {
const res = await fetch(new URL('user/account', endpoint))
assert(!res.ok)
assert.strictEqual(res.status, 401)
})

it('retrieves user account data', async () => {
const db = getDBClient()
const token = 'test-magic'
const user = await db.getUser('test-magic-issuer')
let res, userInfo

// Set PSA access to true and check response
await db.createUserTag(user._id, { tag: 'HasPsaAccess', value: 'true', reason: 'testing' })
res = await fetch(new URL('user/info', endpoint), {
headers: { Authorization: `Bearer ${token}` }
})
userInfo = await res.json()
assert.strictEqual(userInfo.info._id, user._id)
assert.strictEqual(userInfo.info.tags.HasPsaAccess, true)

// Set PSA access to false and check response
await db.createUserTag(user._id, { tag: 'HasPsaAccess', value: 'false', reason: 'testing' })
res = await fetch(new URL('user/info', endpoint), {
headers: { Authorization: `Bearer ${token}` }
})
userInfo = await res.json()
assert.strictEqual(userInfo.info._id, user._id)
assert.strictEqual(userInfo.info.tags.HasPsaAccess, false)
})
})

describe('GET /user/tokens', () => {
it('error if not authenticated with magic.link', async () => {
const token = await getTestJWT()
Expand Down
4 changes: 2 additions & 2 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const userQueryWithTags = `
publicAddress:public_address,
created:inserted_at,
updated:updated_at,
tags:user_tag_user_id_fkey(user_id,id,tag,value)
tags:user_tag_user_id_fkey(user_id,id,tag,value,deleted_at)
`

const psaPinRequestTableName = 'psa_pin_request'
Expand Down Expand Up @@ -173,7 +173,7 @@ export class DBClient {

/**
* Create a user tag
* @param {number} userId
* @param {string} userId
* @param {Object} [tag]
* @param {string} [tag.tag]
* @param {string} [tag.value]
Expand Down

0 comments on commit fdf6c76

Please sign in to comment.