import assert from 'assert' ; import supertest from 'supertest' ; import test from 'unit.js' ; import db from '@mds-core/mds-db' ; import { Geography } from '@mds-core/mds-types' ; import { ApiServer } from '@mds-core/mds-api-server' ; import { POLICY_UUID , GEOGRAPHY_UUID , GEOGRAPHY2_UUID , LA_CITY_BOUNDARY , DISTRICT_SEVEN , SCOPED_AUTH } from '@mds-core/mds-test-data' ; import { api } from '../api' ; import { GEOGRAPHY_API_DEFAULT_VERSION } from '../types' ; const request = supertest ( ApiServer ( api ) ) ; const APP_JSON = 'application/vnd.mds-geography+json; charset=utf-8; version=0.4' ; const EMPTY_SCOPE = SCOPED_AUTH ( [ ] , '' ) ; const EVENTS_READ_SCOPE = SCOPED_AUTH ( [ 'events:read' ] ) ; const GEOGRAPHIES_READ_PUBLISHED_SCOPE = SCOPED_AUTH ( [ 'geographies:read:published' ] ) ; const GEOGRAPHIES_READ_UNPUBLISHED_SCOPE = SCOPED_AUTH ( [ 'geographies:read:unpublished' ] ) ; const GEOGRAPHIES_BOTH_READ_SCOPES = SCOPED_AUTH ( [ 'geographies:read:published' , 'geographies:read:unpublished' ] ) ; describe ( 'Tests app' , ( ) => { describe ( 'Geography endpoint tests' , ( ) => { before ( async ( ) => { await db . initialize ( ) ; } ) ; after ( async ( ) => { await db . shutdown ( ) ; } ) ; it ( 'GETs one unpublished geography with unpublished scope' , async ( ) => { const geography = { name : 'LA' , geography_id : GEOGRAPHY_UUID , geography_json : LA_CITY_BOUNDARY } ; await db . writeGeography ( geography ) ; const result = await request . get ( `/geographies/${ GEOGRAPHY_UUID }` ) . set ( 'Authorization' , GEOGRAPHIES_READ_UNPUBLISHED_SCOPE ) . expect ( 200 ) ; test . assert ( result . body . data . geography . geography_id === GEOGRAPHY_UUID ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; test . value ( result ) . hasHeader ( 'content-type' , APP_JSON ) ; } ) ; it ( 'cannot GET an unpublished geography with the published scope' , done => { request . get ( `/geographies/${ GEOGRAPHY_UUID }` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 403 ) . end ( err => { done ( err ) ; } ) ; } ) ; it ( 'cannot GET a nonexistent geography' , done => { request . get ( `/geographies/${ POLICY_UUID }` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 404 ) . end ( err => { done ( err ) ; } ) ; } ) ; it ( 'cannot GET geographies (no auth)' , done => { request . get ( `/geographies/` ) . set ( 'Authorization' , EMPTY_SCOPE ) . expect ( 403 ) . end ( err => { done ( err ) ; } ) ; } ) ; it ( 'cannot GET geographies (wrong auth)' , done => { request . get ( `/geographies/` ) . set ( 'Authorization' , EVENTS_READ_SCOPE ) . expect ( 403 ) . end ( err => { done ( err ) ; } ) ; } ) ; it ( 'can GET geographies, full version' , done => { request . get ( `/geographies/` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { result . body . data . geographies . forEach ( ( item ) => { test . assert ( item . geography_json ) ; } ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; test . value ( result ) . hasHeader ( 'content-type' , APP_JSON ) ; done ( err ) ; } ) ; } ) ; it ( 'can GET geographies, summarized version' , done => { request . get ( `/geographies?summary=true` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { result . body . data . geographies . forEach ( ( item ) => { test . assert ( ! item . geography_json ) ; } ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; done ( err ) ; } ) ; } ) ; it ( 'can read a published geography with both read scopes' , async ( ) => { await db . writeGeography ( { name : 'Geography 2' , geography_id : GEOGRAPHY2_UUID , geography_json : DISTRICT_SEVEN } ) ; await db . publishGeography ( { geography_id : GEOGRAPHY2_UUID } ) ; await request . get ( `/geographies/${ GEOGRAPHY2_UUID }` ) . set ( 'Authorization' , GEOGRAPHIES_BOTH_READ_SCOPES ) . expect ( 200 ) ; } ) ; it ( 'can GET one unpublished geography with unpublished scope' , done => { request . get ( `/geographies/${ GEOGRAPHY_UUID }` ) . set ( 'Authorization' , GEOGRAPHIES_READ_UNPUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { test . assert ( result . body . data . geography . geography_id === GEOGRAPHY_UUID ) ; test . value ( result ) . hasHeader ( 'content-type' , APP_JSON ) ; done ( err ) ; } ) ; } ) ; it ( 'can get all geographies, with the unpublished scope' , done => { request . get ( `/geographies` ) . set ( 'Authorization' , GEOGRAPHIES_READ_UNPUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { test . assert ( result . body . data . geographies . length === 2 ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; done ( err ) ; } ) ; } ) ; it ( 'can filter for published geographies, with the unpublished scope and get_published parameter' , done => { request . get ( `/geographies?get_published=true` ) . set ( 'Authorization' , GEOGRAPHIES_READ_UNPUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { test . assert ( result . body . data . geographies . length === 1 ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; done ( err ) ; } ) ; } ) ; it ( 'can filter for unpublished geographies, with the unpublished scope and get_unpublished parameter' , done => { request . get ( `/geographies?get_unpublished=true` ) . set ( 'Authorization' , GEOGRAPHIES_READ_UNPUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { assert ( result . body . data . geographies . length === 1 ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; done ( err ) ; } ) ; } ) ; it ( 'can only GET published geographies, with only the published scope' , done => { request . get ( `/geographies` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 200 ) . end ( ( err , result ) => { assert ( result . body . data . geographies . length === 1 ) ; test . assert ( result . body . version === GEOGRAPHY_API_DEFAULT_VERSION ) ; done ( err ) ; } ) ; } ) ; it ( 'throws an error if only the get_published scope is set and get_unpublished param is set' , async ( ) => { await request . get ( `/geographies?get_unpublished=true` ) . set ( 'Authorization' , GEOGRAPHIES_READ_PUBLISHED_SCOPE ) . expect ( 403 ) ; } ) ; } ) ; } ) ;