Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mds-policy] Adding data envelop back to mds-policy and correcting version number #334

Merged
merged 5 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/mds-policy/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function api(app: express.Express): express.Express {
throw new NotFoundError('No policies found!')
}

res.status(200).send({ version: res.locals.version, policies: active })
res.status(200).send({ version: res.locals.version, data: { policies: active } })
} catch (error) {
if (error instanceof NotFoundError) {
return res.status(404).send({ error })
Expand Down Expand Up @@ -143,7 +143,7 @@ function api(app: express.Express): express.Express {

const [policy] = policies

res.status(200).send({ version: res.locals.version, policy })
res.status(200).send({ version: res.locals.version, data: { policy } })
} catch (error) {
if (error instanceof BadParamsError) {
return res.status(400).send({ error })
Expand Down
27 changes: 14 additions & 13 deletions packages/mds-policy/tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const log = console.log.bind(console)

const request = supertest(ApiServer(api))

const APP_JSON = 'application/vnd.mds.policy+json; charset=utf-8; version=0.1'
const APP_JSON = 'application/vnd.mds.policy+json; charset=utf-8; version=0.4'

const AUTH = `basic ${Buffer.from(`${TEST1_PROVIDER_ID}|${PROVIDER_SCOPES}`).toString('base64')}`
const POLICIES_READ_SCOPE = SCOPED_AUTH(['policies:read'])
Expand Down Expand Up @@ -84,18 +84,18 @@ describe('Tests app', () => {
log('read back one policy response:', body)
test.value(body.version).is(POLICY_API_DEFAULT_VERSION)
test.value(result).hasHeader('content-type', APP_JSON)
// TODO verify contents
test.value(result.body.data.policy.policies, POLICY_UUID)
})

it('reads back all active policies', async () => {
const result = await request.get(`/policies`).set('Authorization', AUTH).expect(200)
const body = result.body
log('read back all policies response:', body)
test.value(body.policies.length).is(1) // only one should be currently valid
test.value(body.policies[0].policy_id).is(POLICY_UUID)
test.value(body.data.policies.length).is(1) // only one should be currently valid
test.value(body.data.policies[0].policy_id).is(POLICY_UUID)
test.value(body.version).is(POLICY_API_DEFAULT_VERSION)
test.value(result).hasHeader('content-type', APP_JSON)
// TODO verify contents
test.value(result.body.data.policies, [POLICY_JSON])
})

it('read back all published policies and no superseded ones', async () => {
Expand All @@ -117,14 +117,15 @@ describe('Tests app', () => {
.set('Authorization', AUTH)
.expect(200)
const body = result.body
const policies = result.body.data.policies
log('read back all published policies response:', body)
test.value(body.policies.length).is(3)
test.value(policies.length).is(3)
test.value(body.version).is(POLICY_API_DEFAULT_VERSION)
test.value(result).hasHeader('content-type', APP_JSON)
const isSupersededPolicyPresent = body.policies.some((policy: Policy) => {
const isSupersededPolicyPresent = policies.some((policy: Policy) => {
return policy.policy_id === POLICY_JSON.policy_id
})
const isSupersedingPolicyPresent = body.policies.some((policy: Policy) => {
const isSupersedingPolicyPresent = policies.some((policy: Policy) => {
return policy.policy_id === SUPERSEDING_POLICY_JSON.policy_id
})
test.value(isSupersededPolicyPresent).is(false)
Expand All @@ -137,12 +138,12 @@ describe('Tests app', () => {
.set('Authorization', AUTH)
.expect(200)
const body = result.body
const policies = body.data.policies
log('read back all policies response:', body)
test.value(body.policies.length).is(1) // only one
test.value(body.policies[0].policy_id).is(POLICY2_UUID)
test.value(policies.length).is(1) // only one
test.value(policies[0].policy_id).is(POLICY2_UUID)
test.value(body.version).is(POLICY_API_DEFAULT_VERSION)
test.value(result).hasHeader('content-type', APP_JSON)
// TODO verify contents
})

it('read back current and future policies', async () => {
Expand All @@ -152,7 +153,7 @@ describe('Tests app', () => {
.expect(200)
const body = result.body
log('read back all policies response:', body)
test.value(body.policies.length).is(2) // current and future
test.value(body.data.policies.length).is(2) // current and future
test.value(body.version).is(POLICY_API_DEFAULT_VERSION)
test.value(result).hasHeader('content-type', APP_JSON)
})
Expand All @@ -178,7 +179,7 @@ describe('Tests app', () => {
.get(`/policies?get_unpublished=true`)
.set('Authorization', POLICIES_READ_SCOPE)
.expect(200)
test.assert(result.body.policies.length === 1)
test.assert(result.body.data.policies.length === 1)
})

it('can GET one unpublished policy', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/mds-policy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { ApiRequest, ApiVersionedResponse, ApiClaims } from '@mds-core/mds-api-server'
import { Policy } from '@mds-core/mds-types'

export const POLICY_API_SUPPORTED_VERSIONS = ['0.1.0'] as const
export const POLICY_API_SUPPORTED_VERSIONS = ['0.4.1'] as const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 0.4.1 the first/only supported version?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, yes.

export type POLICY_API_SUPPORTED_VERSION = typeof POLICY_API_SUPPORTED_VERSIONS[number]
export const [POLICY_API_DEFAULT_VERSION] = POLICY_API_SUPPORTED_VERSIONS

Expand All @@ -31,5 +31,5 @@ export type PolicyApiResponse<TBody = {}> = ApiVersionedResponse<
TBody
>

export type GetPolicyResponse = PolicyApiResponse<{ policy: Policy }>
export type GetPoliciesResponse = PolicyApiResponse<{ policies: Policy[] }>
export type GetPolicyResponse = PolicyApiResponse<{ data: { policy: Policy } }>
export type GetPoliciesResponse = PolicyApiResponse<{ data: { policies: Policy[] } }>