|
| 1 | +var request = require('supertest') |
| 2 | +var loopback = require('loopback') |
| 3 | +var expect = require('chai').expect |
| 4 | +var JSONAPIComponent = require('../') |
| 5 | +var app |
| 6 | +var Post |
| 7 | +var Comment |
| 8 | + |
| 9 | +describe('foreign key configuration', function () { |
| 10 | + beforeEach(function (done) { |
| 11 | + app = loopback() |
| 12 | + app.set('legacyExplorer', false) |
| 13 | + var ds = loopback.createDataSource('memory') |
| 14 | + |
| 15 | + Post = ds.createModel('post', { title: String }) |
| 16 | + app.model(Post) |
| 17 | + |
| 18 | + Comment = ds.createModel('comment', { comment: String }) |
| 19 | + app.model(Comment) |
| 20 | + |
| 21 | + Comment.belongsTo(Post) |
| 22 | + Post.hasMany(Comment) |
| 23 | + |
| 24 | + app.use(loopback.rest()) |
| 25 | + |
| 26 | + Post.create({title: 'my post'}, function (err, post) { |
| 27 | + if (err) throw err |
| 28 | + post.comments.create({comment: 'my comment'}, done) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('by default, foreign keys are not exposed through the api', function () { |
| 33 | + beforeEach(function () { |
| 34 | + JSONAPIComponent(app) |
| 35 | + }) |
| 36 | + it('should remove foreign keys from model before output', function (done) { |
| 37 | + request(app).get('/comments/1') |
| 38 | + .end(function (err, res) { |
| 39 | + expect(err).to.equal(null) |
| 40 | + expect(res.body.data.attributes).to.not.include.key('postId') |
| 41 | + done() |
| 42 | + }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('configuring component to always expose foreign keys through the api', function () { |
| 47 | + beforeEach(function () { |
| 48 | + JSONAPIComponent(app, { |
| 49 | + foreignKeys: true |
| 50 | + }) |
| 51 | + }) |
| 52 | + it('should not remove foreign keys from models before output', function (done) { |
| 53 | + request(app).get('/comments/1') |
| 54 | + .end(function (err, res) { |
| 55 | + expect(err).to.equal(null) |
| 56 | + expect(res.body.data.attributes).to.include.key('postId') |
| 57 | + done() |
| 58 | + }) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('configuring component to expose foreign keys for post model through the api', function () { |
| 63 | + beforeEach(function () { |
| 64 | + JSONAPIComponent(app, { |
| 65 | + foreignKeys: [ |
| 66 | + {model: 'post'} |
| 67 | + ] |
| 68 | + }) |
| 69 | + }) |
| 70 | + it('should not expose postId on comment model', function (done) { |
| 71 | + request(app).get('/comments/1') |
| 72 | + .end(function (err, res) { |
| 73 | + expect(err).to.equal(null) |
| 74 | + expect(res.body.data.attributes).to.not.include.key('postId') |
| 75 | + done() |
| 76 | + }) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('configuring component to expose foreign keys for comment model through the api', function () { |
| 81 | + beforeEach(function () { |
| 82 | + JSONAPIComponent(app, { |
| 83 | + foreignKeys: [ |
| 84 | + {model: 'comment'} |
| 85 | + ] |
| 86 | + }) |
| 87 | + }) |
| 88 | + it('should expose postId on comment model', function (done) { |
| 89 | + request(app).get('/comments/1') |
| 90 | + .end(function (err, res) { |
| 91 | + expect(err).to.equal(null) |
| 92 | + expect(res.body.data.attributes).to.include.key('postId') |
| 93 | + done() |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('configuring component to expose foreign keys for comment model method findById through the api', function () { |
| 99 | + beforeEach(function () { |
| 100 | + JSONAPIComponent(app, { |
| 101 | + foreignKeys: [ |
| 102 | + {model: 'comment', method: 'findById'} |
| 103 | + ] |
| 104 | + }) |
| 105 | + }) |
| 106 | + it('should not expose foreign keys in find all', function (done) { |
| 107 | + request(app).get('/comments') |
| 108 | + .end(function (err, res) { |
| 109 | + expect(err).to.equal(null) |
| 110 | + expect(res.body.data[0].attributes).to.not.include.key('postId') |
| 111 | + done() |
| 112 | + }) |
| 113 | + }) |
| 114 | + it('should expose foreign keys in find', function (done) { |
| 115 | + request(app).get('/comments/1') |
| 116 | + .end(function (err, res) { |
| 117 | + expect(err).to.equal(null) |
| 118 | + expect(res.body.data.attributes).to.include.key('postId') |
| 119 | + done() |
| 120 | + }) |
| 121 | + }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments