| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 'use strict'
-
- const test = require('ava')
- const { stub } = require('sinon')
- const Hapi = require('@hapi/hapi')
-
- const plugin = require('../lib/plugins/membership')
-
- const ProfileService = require('../lib/services/profile')
- const UserService = require('../lib/services/user')
- const MembershipService = require('../lib/services/membership')
-
- const Profile = require('../lib/models/profile')
- const Grouping = require('../lib/models/grouping')
- const Membership = require('../lib/models/membership')
- const Tag = require('../lib/models/tag')
- const TagAssociation = require('../lib/models/tag-association')
- const User = require('../lib/models/user')
- const ZipCode = require('../lib/models/zip-code')
- const Aspect = require('../lib/models/aspect')
- const AspectLabel = require('../lib/models/aspect_label')
-
- /**
- * Route parameters
- */
- const params = {
- profile_id: 99,
- }
- const mockReturn = {
- profile: {
- profile_id: 99,
- user_id: 99,
- user: {
- user_email: '',
- user_name: 'bob',
- },
- responses: [],
- tags: [],
- },
- memberships: [
- { membership_id: 1, grouping_id: 1, profile_id: 99 },
- { membership_id: 1, grouping_id: 1, profile_id: 1 },
- { membership_id: 1, grouping_id: 1, profile_id: 2 },
- ],
- groupings: [
- { grouping_id: 1, profiles: [{ profile_id: 1 }, { profile_id: 99 }] },
- ],
- profiles: [
- {
- profile_id: 1,
- tags: [],
- responses: [
- { val: '90012', response_key_id: 7 },
- { val: 'foo', response_key_id: 8 },
- { val: 'foo', response_key_id: 9 },
- { val: 'foo', response_key_id: 10 },
- { val: 'foo', response_key_id: 11 },
- { val: 'foo', response_key_id: 12 },
- ],
- user: { user_name: 'phil' },
- },
- {
- profile_id: 2,
- tags: [],
- responses: [
- { val: '90012', response_key_id: 7 },
- { val: 'foo', response_key_id: 8 },
- { val: 'foo', response_key_id: 9 },
- { val: 'foo', response_key_id: 10 },
- { val: 'foo', response_key_id: 11 },
- { val: 'foo', response_key_id: 12 },
- ],
- user: { user_name: 'gil' },
- },
- {
- profile_id: 99,
- tags: [],
- responses: [
- { val: '90012', response_key_id: 7 },
- { val: 'foo', response_key_id: 8 },
- { val: 'foo', response_key_id: 9 },
- { val: 'foo', response_key_id: 10 },
- { val: 'foo', response_key_id: 11 },
- { val: 'foo', response_key_id: 12 },
- ],
- user: { user_name: 'jill' },
- },
- ],
- tag_associations: [
- {
- tag_association_id: 1,
- profile_id: 99,
- grouping_id: 1,
- tag_id: 7,
- is_deleted: 0,
- tag: {
- tag_category: 'reveal',
- },
- },
- {
- tag_association_id: 2,
- profile_id: 1,
- grouping_id: 1,
- tag_id: 7,
- is_deleted: 0,
- tag: {
- tag_category: 'reveal',
- },
- },
- ],
- tags: [],
- labels: [
- { aspect_id: 1, a: 100, b: 100 },
- { aspect_id: 2, a: 100, b: 200 },
- { aspect_id: 3, a: 200, b: 200 },
- { aspect_id: 4, a: 200, b: 100 },
- ],
- aspects: [
- { aspect_id: 1, 1: 111, 2: 112, 3: 113, 4: 114 },
- { aspect_id: 2, 1: 222, 2: 222, 3: 333, 4: 222 },
- { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 },
- { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 },
- ],
- user: [
- {
- user_id: 99,
- user_name: 'bob',
- user_email: 'bob@testemail.com',
- is_admin: 0,
- is_poster: 0,
- is_verified: 0,
- },
- ],
- }
- const pathToTest = {
- method: 'GET',
- url: `/${params.profile_id}`,
- }
-
- test('path /<profile_id> should return ok', async t => {
- /**
- * Create a new server and register services,
- * models and routes for testing
- * -
- * NOTE: We use a mocked registerModel() and register
- * models manually. Normally this is handled by
- * Schwifty at runtime.
- */
- const server = Hapi.server()
- /**
- * Overload so we don't register any models
- * using the plugin call (see plugins/profile.js)
- * and Manually load the model we need for the test
- */
- server.registerModel = () => {}
- server.models = () => ({
- Aspect,
- AspectLabel,
- Profile,
- Membership,
- Grouping,
- Tag,
- TagAssociation,
- User,
- })
-
- // server.registerService(ProfileService)
- // server.registerService(MembershipService)
- // server.registerService(UserService)
- /**
- * Register Routes and Services as usual
- */
- await plugin.register(server)
- /**
- * Replace Objection model methods with our own mock functions
- * !: Janky - might be better to temp knex sqlite instance
- */
- server.services()['profileService'] = new ProfileService(server)
- server.services()['membershipService'] = new MembershipService(server)
- server.services()['userService'] = new UserService(server)
-
- stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
- stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
- stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
- stub(server.models()['User'], 'query').returns({
- throwIfNotFound: () => ({
- first: () => ({
- where: () => {
- return mockReturn.user
- },
- }),
- }),
- })
- stub(server.models()['Grouping'], 'query').returns({
- whereIn: () => ({
- withGraphFetched: () => {
- return mockReturn.groupings
- },
- }),
- })
- stub(server.models()['Membership'], 'query').returns({
- where: () => {
- return mockReturn.memberships
- },
- whereIn: () => {
- return mockReturn.memberships
- },
- })
- stub(server.models()['TagAssociation'], 'query').returns({
- where: () => ({
- andWhere: () => {
- return mockReturn.tag_associations
- },
- }),
- })
- stub(server.models()['Profile'], 'query').returns({
- // Mocked for getProfile()
- where: () => ({
- first: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => mockReturn.profile,
- }),
- }),
- }),
- }),
- whereIn: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => mockReturn.profiles,
- }),
- }),
- }),
- })
-
- /**
- * Test the server with registered models and services
- */
- const { payload } = await server.inject(pathToTest)
- const res = JSON.parse(payload)
-
- t.log('res :=>', res)
- t.deepEqual(res.ok, true)
- t.deepEqual(res.data.length, 1)
-
- /*
- t.deepEqual(res.data[0].grouping_id, mockReturn.groupings[0].grouping_id)
- t.deepEqual(
- res.data[0].profile.user_name,
- mockReturn.groupings[0].profile.user_name,
- )
- */
- })
|