Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SPD): Implement scottish postcode lookup
- Loading branch information
Showing
4 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use strict"; | ||
|
||
const ScottishPostcode = require("../models/scottish_postcode"); | ||
const Postcode = require("postcode"); | ||
const { | ||
InvalidPostcodeError, | ||
PostcodeNotFoundError, | ||
} = require("../lib/errors.js"); | ||
|
||
exports.show = (request, response, next) => { | ||
const { postcode } = request.params; | ||
if (!Postcode.isValid(postcode.trim())) | ||
return next(new InvalidPostcodeError()); | ||
|
||
ScottishPostcode.find(postcode, (error, result) => { | ||
if (error) return next(error); | ||
if (!result) return next(new PostcodeNotFoundError()); | ||
response.jsonApiResponse = { | ||
status: 200, | ||
result: ScottishPostcode.toJson(result), | ||
}; | ||
return next(); | ||
}); | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"use strict"; | ||
|
||
const request = require("supertest"); | ||
const { assert } = require("chai"); | ||
const { | ||
postcodesioApplication, | ||
clearScottishPostcodeDb, | ||
seedScottishPostcodeDb, | ||
} = require("./helper"); | ||
const app = postcodesioApplication(); | ||
const Postcode = require("postcode"); | ||
|
||
const error404Message = "Postcode not found"; | ||
|
||
describe("Scottish postcode route", () => { | ||
const testPostcode = "ML11 0GH"; | ||
|
||
before(function(done) { | ||
this.timeout(0); | ||
clearScottishPostcodeDb((error, result) => { | ||
if (error) return done(error); | ||
seedScottishPostcodeDb((error, result) => { | ||
if (error) return done(error); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
after(clearScottishPostcodeDb); | ||
|
||
describe("/GET /scotland/postcodes/:postcode", () => { | ||
it("should return 200 if postcode found", done => { | ||
const path = `/scotland/postcodes/${encodeURI(testPostcode)}`; | ||
request(app) | ||
.get(path) | ||
.expect("Content-Type", /json/) | ||
.expect(200) | ||
.end((error, response) => { | ||
if (error) return done(error); | ||
assert.equal(response.body.status, 200); | ||
assert.equal(Object.keys(response.body).length, 2); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("returns the correct attributes", done => { | ||
const path = `/scotland/postcodes/${encodeURI(testPostcode)}`; | ||
request(app) | ||
.get(path) | ||
.expect("Content-Type", /json/) | ||
.expect(200) | ||
.end((error, response) => { | ||
if (error) return done(error); | ||
const { result } = response.body; | ||
assert.deepEqual(result, { | ||
codes: { | ||
scottish_parliamentary_constituency: "S16000090", | ||
}, | ||
postcode: "ML11 0GH", | ||
scottish_parliamentary_constituency: "Clydesdale", | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("accepts padded postcode", done => { | ||
const postcode = " " + testPostcode + " "; | ||
const path = `/scotland/postcodes/${encodeURI(postcode)}`; | ||
request(app) | ||
.get(path) | ||
.expect("Content-Type", /json/) | ||
.expect(200) | ||
.end((error, response) => { | ||
if (error) return done(error); | ||
assert.equal(response.body.status, 200); | ||
const { result } = response.body; | ||
assert.deepEqual(result, { | ||
codes: { | ||
scottish_parliamentary_constituency: "S16000090", | ||
}, | ||
postcode: "ML11 0GH", | ||
scottish_parliamentary_constituency: "Clydesdale", | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("404 if not a valid postcode according to the postcode module", done => { | ||
const path = `/scotland/postcodes/foo`; | ||
assert.isFalse(Postcode.isValid("foo")); | ||
request(app) | ||
.get(path) | ||
.expect("Content-Type", /json/) | ||
.expect(404) | ||
.end(done); | ||
}); | ||
|
||
it("should return 404 if postcode not found", done => { | ||
const postcode = "ID11QE"; | ||
const path = `/scotland/postcodes/${encodeURI(postcode)}`; | ||
request(app) | ||
.get(path) | ||
.expect("Content-Type", /json/) | ||
.expect(404) | ||
.end((error, response) => { | ||
if (error) return done(error); | ||
assert.property(response.body, "status"); | ||
assert.equal(response.body.status, 404); | ||
assert.property(response.body, "error"); | ||
assert.equal(Object.keys(response.body).length, 2); | ||
assert.equal(response.body.error, error404Message); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |