Created
July 12, 2017 09:57
-
-
Save ZiiSolutions/ec99933ea305a99e0b7053df5c763515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const mocha = require('mocha'); | |
const chai = require('chai'); | |
const sinon = require('sinon'); | |
const expect = chai.expect; | |
const app = require('../../app'); | |
const express = require('express'); | |
const request = require('supertest'); | |
const ingestService = require('../../lib/services/ingest/image-metadata-ingest-service'); | |
let sandbox; | |
function getTestJson(requireEntityUri) { | |
if (requireEntityUri) { | |
return { | |
id: "e17f96c2-f02c-428d-845d-6a10758e82f0", | |
timestamp: "2017-04-06T13:20:16Z", | |
key: "item.create.2.30123321", | |
domain: "images", | |
type: "item", | |
operation: "create", | |
entity: { | |
uri: "2.30123321", | |
version: "1", | |
type: "picture", | |
profile: "picture", | |
firstcreated: "2017-04-06T13:20:08Z", | |
versioncreated: "2017-04-06T13:20:08Z", | |
urgency: 6, | |
pubstatus: "usable", | |
object: [ | |
{ | |
code: "paimagescopyright:226", | |
rel: "classifiedAs", | |
scheme: "http://image-metadata-repository.api.press.net/v1/object", | |
name: "Image copyright code" | |
} | |
], | |
subject: [ | |
{ | |
code: "paimagescategory:s" | |
}, | |
{ | |
code: "paimagescategory:supplemental:motorcycling" | |
} | |
] | |
}, | |
origin: { | |
name: "repository/image-metadata-repository/v1", | |
address: "10.10.10.10", | |
port: 3097 | |
} | |
}; | |
} else { | |
return { | |
id: "e17f96c2-f02c-428d-845d-6a10758e82f0", | |
timestamp: "2017-04-06T13:20:16Z", | |
key: "item.create.2.30123321", | |
domain: "images", | |
type: "item", | |
operation: "create", | |
entity: { | |
version: "1", | |
type: "picture", | |
profile: "picture", | |
firstcreated: "2017-04-06T13:20:08Z", | |
versioncreated: "2017-04-06T13:20:08Z", | |
urgency: 6, | |
pubstatus: "usable", | |
object: [ | |
{ | |
code: "paimagescopyright:226", | |
rel: "classifiedAs", | |
scheme: "http://image-metadata-repository.api.press.net/v1/object", | |
name: "Image copyright code" | |
} | |
], | |
subject: [ | |
{ | |
code: "paimagescategory:s" | |
}, | |
{ | |
code: "paimagescategory:supplemental:motorcycling" | |
} | |
] | |
}, | |
origin: { | |
name: "repository/image-metadata-repository/v1", | |
address: "10.10.10.10", | |
port: 3097 | |
} | |
}; | |
} | |
} | |
function performRequest(json) { | |
return request(app) | |
.post('/ingest/content/image-metadata/v1/notify') | |
.send(json); | |
} | |
describe('controllers/app#notify', function () { | |
beforeEach(function (done) { | |
sandbox = sinon.sandbox.create(); | |
done(); | |
}); | |
afterEach(function (done) { | |
sandbox.restore(); | |
done(); | |
}); | |
it('should take a valid post and expect a 202 http status code', function (done) { | |
const postJson = getTestJson(true); | |
sandbox.stub(ingestService, 'post') | |
.resolves('foo'); | |
performRequest(postJson) | |
.expect(202, done); | |
}); | |
it('should take an invalid post and return 400 http status code', function (done) { | |
const postJson = getTestJson(false); | |
performRequest(postJson) | |
.expect(400, done); | |
}); | |
it('should return HTTP_BAD_REQUEST(400) when the images API returns an error', function (done) { | |
const postJson = getTestJson(false); | |
sandbox.stub(ingestService, 'post') | |
.rejects(new Error('some error')); | |
performRequest(postJson) | |
.expect(400, done); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment