Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Merge branch 'dev' of github.com:data-studio/user-svc into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Callan Milne committed Sep 6, 2017
2 parents 54e9b07 + 77833d5 commit 8b682fc
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 9 deletions.
106 changes: 106 additions & 0 deletions spec/api/35-app-schema-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

describe("APP_SCHEMA REST Schema", function () {

let schema;

let userId;
let authorization;

let login;
let password;

let $testClient;

beforeEach(function (done) {

schema = jasmine.startTestApi();
$testClient = jasmine.createTestClient();

login = $testClient.uniqueLogin();
password = $testClient.generatePassword();

$testClient.initUser(login, password, function (err, d) {
if (err) return done(err);
userId = d.UserId;
authorization = d.TokenKey;
done();
});

});

afterEach(function (done) {
schema.server.close(done);
});

describe("/app/:appId/schemas", function () {

let appData;
let app;
let appId;

let schemaData;

beforeEach(function (done) {
schemaData = {
Name: "My Test Schema",
};
appData = {
Name: "My Test App",
};
$testClient.$post(authorization, `/apps`, appData, function (err, res) {
$testClient.$get(authorization, res.headers.location, function (err, res) {
app = res.d;
appId = app.Id;
done();
});
});
});

describe("createAppSchema <POST> with valid parameters", function () {

it("RETURNS `HTTP/1.1 403 Forbidden` WHEN `Authorization` HEADER IS NOT PROVIDED", function (done) {
$testClient.$post(null, `/app/${appId}/schemas`, schemaData, function (err, res) {
expect(res.statusCode).toBe(403);
done();
});
});

it("RETURNS `HTTP/1.1 303 See Other` WHEN `Authorization` HEADER IS PROVIDED", function (done) {
$testClient.$post(authorization, `/app/${appId}/schemas`, schemaData, function (err, res) {
expect(res.statusCode).toBe(303);
expect(res.headers.location).toMatch(jasmine.idUrlRegexp("app", "schema"));
done();
});
});

it("CREATES AN APP SCHEMA", function (done) {
$testClient.$post(authorization, `/app/${appId}/schemas`, schemaData, function (err, res) {
$testClient.$get(authorization, res.headers.location, function (err, res) {
expect(res.statusCode).toBe(200);
done();
});
});
});

it("ADDS THE SCHEMA TO THE APPS LIST OF SCHEMAS", function (done) {
$testClient.$post(authorization, `/app/${appId}/schemas`, schemaData, function (err, res) {
let schemaId = res.headers.location.split(/\//g).pop();
$testClient.$get(authorization, `/app/${appId}`, function (err, res) {
expect(res.statusCode).toBe(200);
expect(res.d).toEqual(jasmine.objectContaining({
"Schemas": jasmine.arrayContaining([
jasmine.objectContaining({
"Id": schemaId
}),
]),
}));
done();
});
});
});

});

});

});
116 changes: 116 additions & 0 deletions spec/api/40-schema-property-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

describe("SCHEMA_PROPERTY REST API", function () {

let api;

let userId;
let authorization;

let login;
let password;

let $testClient;

beforeEach(function (done) {

api = jasmine.startTestApi();
$testClient = jasmine.createTestClient();

login = $testClient.uniqueLogin();
password = $testClient.generatePassword();

$testClient.initUser(login, password, function (err, d) {
if (err) return done(err);
userId = d.UserId;
authorization = d.TokenKey;
done();
});

});

afterEach(function (done) {
api.server.close(done);
});

describe("/schema/:schemaId/properties", function () {

let app;
let schema;
let appData;
let schemaData;
let appId;
let schemaId;

let propertyData;

beforeEach(function (done) {
appData = {
Name: "My Test App",
};
schemaData = {
Name: "My Test API",
};
propertyData = {
Key: "MyProp",
};
$testClient.$post(authorization, `/apps`, appData, function (err, res) {
$testClient.$get(authorization, res.headers.location, function (err, res) {
app = res.d;
appId = app.Id;
$testClient.$post(authorization, `/app/${appId}/schemas`, schemaData, function (err, res) {
$testClient.$get(authorization, res.headers.location, function (err, res) {
schema = res.d;
schemaId = schema.Id;
done();
});
});
});
});
});

describe("createSchemaProperty <POST> with valid parameters", function () {

it("RETURNS `HTTP/1.1 403 Forbidden` WHEN `Authorization` HEADER IS NOT PROVIDED", function (done) {
$testClient.$post(null, `/schema/${schemaId}/properties`, propertyData, function (err, res) {
expect(res.statusCode).toBe(403);
done();
});
});

it("RETURNS `HTTP/1.1 303 See Other` WHEN `Authorization` HEADER IS PROVIDED", function (done) {
$testClient.$post(authorization, `/schema/${schemaId}/properties`, propertyData, function (err, res) {
expect(res.statusCode).toBe(303);
expect(res.headers.location).toMatch(jasmine.idUrlRegexp("schema", "property"));
done();
});
});

it("CREATES AN API PROPERTY", function (done) {
$testClient.$post(authorization, `/schema/${schemaId}/properties`, propertyData, function (err, res) {
$testClient.$get(authorization, res.headers.location, function (err, res) {
expect(res.statusCode).toBe(200);
done();
});
});
});

it("ADDS THE PROPERTY TO THE SCHEMAS LIST OF PROPERTIES", function (done) {
$testClient.$post(authorization, `/schema/${schemaId}/properties`, propertyData, function (err, res) {
let propId = res.headers.location.split(/\//g).pop();
$testClient.$get(authorization, `/schema/${schemaId}/properties`, function (err, res) {
expect(res.statusCode).toBe(200);
expect(res.d).toEqual(jasmine.arrayContaining([
jasmine.objectContaining({
"Id": propId
}),
]));
done();
});
});
});

});

});

});
1 change: 1 addition & 0 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function () {
"Auth",
"Client",
"Hash",
"Property",
"ResourceOwner",
"Resource",
"Route",
Expand Down
10 changes: 8 additions & 2 deletions src/db/Api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ module.exports = function ApiDb (db) {
return this.belongsTo(db.App, "AppId", "Id");
},
Routes: function() {
return this.hasMany(db.Route, "ApiId", "Id");
return this.hasMany(db.Route, "ApiId", "Id")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
Operations: function() {
return this.hasMany(db.Operation, "ApiId", "Id");
return this.hasMany(db.Operation, "ApiId", "Id")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
});

Expand Down
15 changes: 12 additions & 3 deletions src/db/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ module.exports = function AppDb (db) {
return this.belongsTo(db.User, "UserId", "Id");
},
Schemas: function() {
return this.hasMany(db.AppSchema, "AppId");
return this.hasMany(db.AppSchema, "AppId")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
Clients: function() {
return this.hasMany(db.Client, "AppId");
return this.hasMany(db.Client, "AppId")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
Apis: function() {
return this.hasMany(db.Api, "AppId");
return this.hasMany(db.Api, "AppId")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/db/Operation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function OperationDb (db) {

function fetchOperationsByApiId (apiId) {
return new Promise((resolve, reject) => {
Operation.where({"ApiId": apiId})
Operation.where({"ApiId": apiId, "Deleted": null})
.fetchAll()
.then(resolve)
.catch(reject);
Expand Down
55 changes: 55 additions & 0 deletions src/db/Property/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


module.exports = function Property (db) {

const bookshelf = db._bookshelf;

let Property = bookshelf.Model.extend({
tableName: 'schema_properties',
constructor: function() {
bookshelf.Model.apply(this, arguments);
this.on('saving', function(model, attrs, options) {
options.query.where('Id', '=', model.get("Id"));
});
},
Schema: function() {
return this.belongsTo(db.Schema, "SchemaId", "Id");
},
});

db.Property = Property;

function fetchPropertyById (id) {
return new Promise((resolve, reject) => {
Property.where({"Id": id})
.fetch({withRelated: ["Schema"]})
.then(resolve)
.catch(reject);
});
};

db.fetchPropertyById = fetchPropertyById;

function fetchPropertiesBySchemaId (schemaId) {
return new Promise((resolve, reject) => {
Property.where({"SchemaId": schemaId, "Deleted": null})
.fetchAll()
.then(resolve)
.catch(reject);
});
}

db.fetchPropertiesBySchemaId = fetchPropertiesBySchemaId;

function fetchPropertyById (id) {
return new Promise((resolve, reject) => {
Property.where({"Id": id, "Deleted": null})
.fetch()
.then(resolve)
.catch(reject);
});
}

db.fetchPropertyById = fetchPropertyById;

};
8 changes: 7 additions & 1 deletion src/db/Schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ module.exports = function Schema (db) {
App: function() {
return this.belongsTo(db.App, "AppId", "Id");
},
Properties: function() {
return this.hasMany(db.Property, "Id", "SchemaId")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
});

db.Schema = Schema;

function fetchSchemaById (id) {
return new Promise((resolve, reject) => {
Schema.where({"Id": id})
.fetch({withRelated: ["App", "Operations"]})
.fetch({withRelated: ["App", "Properties"]})
.then(resolve)
.catch(reject);
});
Expand Down
5 changes: 4 additions & 1 deletion src/db/User/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ module.exports = function UserDb (db) {
});
},
Apps: function() {
return this.hasMany(db.App, "Id", "UserId");
return this.hasMany(db.App, "Id", "UserId")
.query(function(qb) {
qb.whereNull('Deleted');
});
},
});

Expand Down
15 changes: 15 additions & 0 deletions src/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ module.exports = function (dataStudio) {

});

api.param("schemaId", function (req, res, next, id) {

req.schemaModel = null;

db.fetchSchemaById(id, { withRelated: ["Schema"] })
.then(function (schema) {
req.schemaModel = schema;
next();
})
.catch(function (err) {
next();
});

});

api.param("operationId", function (req, res, next, id) {

req.operationModel = null;
Expand Down
Loading

0 comments on commit 8b682fc

Please sign in to comment.