Skip to content

Commit

Permalink
Merge 7bcd592 into 7a4c641
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis authored Sep 22, 2021
2 parents 7a4c641 + 7bcd592 commit e79ce4a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 79 deletions.
7 changes: 6 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ function createApp(isSecure: boolean): express.Application {
extended: false,
})
);
app.use(bodyParser.json({ limit: '1mb' }));
app.use(
bodyParser.json({
limit: '1mb',
strict: false,
})
);

// Use fileUpload to handle multi-part uploads
app.use(fileUpload());
Expand Down
31 changes: 5 additions & 26 deletions src/controllers/actions_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,47 +95,26 @@ function build(): express.Router {
*/
controller.post('/:actionName', async (request, response) => {
const actionName = request.params.actionName;

const keys = Object.keys(request.body);
if (keys.length != 1) {
const err = 'Incorrect number of parameters.';
console.log(err, request.body);
response.status(400).send(err);
return;
}

if (actionName !== keys[0]) {
const err = `Action name must be ${actionName}`;
console.log(err, request.body);
response.status(400).send(err);
return;
}

if (!Object.prototype.hasOwnProperty.call(request.body[actionName], 'input')) {
response.status(400).send('Missing input');
return;
}

const actionParams = request.body[actionName].input;
const input = request.body;
const thingId = request.params.thingId;
let action = null;

if (thingId) {
try {
const thing = await Things.getThing(thingId);
action = new Action(actionName, actionParams, thing);
action = new Action(actionName, input, thing);
} catch (e) {
console.error('Thing does not exist', thingId, e);
response.status(404).send(e);
return;
response.status(404).send(e);
}
} else {
action = new Action(actionName, actionParams);
action = new Action(actionName, input);
}

try {
if (thingId) {
await AddonManager.requestAction(thingId, action.getId(), actionName, actionParams);
await AddonManager.requestAction(thingId, action.getId(), actionName, input);
}
await Actions.add(action);

Expand Down
4 changes: 4 additions & 0 deletions src/models/thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ export default class Thing extends EventEmitter {
return this.properties;
}

getActions(): Record<string, ActionSchema> {
return this.actions;
}

getEvents(): Record<string, EventSchema> {
return this.events;
}
Expand Down
19 changes: 1 addition & 18 deletions src/test/integration/actions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ describe('actions/', () => {
expect(err.status).toEqual(400);
});

it('should fail to create a new action (wrong name)', async () => {
const descr = {
potato: {},
};
const err = await chai
.request(server)
.post(`${Constants.ACTIONS_PATH}/pair`)
.set(...headerAuth(jwt))
.set('Accept', 'application/json')
.send(descr);
expect(err.status).toEqual(400);
});

it('should fail when plugin rejects requestAction', async () => {
const { id } = thingLight;
await addDevice(thingLight);
Expand Down Expand Up @@ -185,11 +172,7 @@ describe('actions/', () => {

it('should list and retrieve the new action by name', async () => {
const descr = {
pair: {
input: {
timeout: 60,
},
},
timeout: 60,
};

const pair = await chai
Expand Down
2 changes: 1 addition & 1 deletion static/js/schema-impl/action/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ActionDetail {
handleClick() {
const input = {};
if (typeof this.input === 'undefined') {
API.postJson(this.href, { [this.name]: { input } }).catch((e) => {
API.postJson(this.href, input).catch((e) => {
console.error(`Error performing action "${this.name}": ${e}`);
});
} else {
Expand Down
47 changes: 24 additions & 23 deletions static/js/schema-impl/capability/thing.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,33 +297,34 @@ class Thing {
if (format === Constants.ThingFormat.EXPANDED) {
// Parse actions
if (description.actions) {
let href;
for (const link of description.links) {
if (link.rel === 'actions') {
href = link.href;
break;
for (const name in description.actions) {
const action = description.actions[name];

let href;
for (const link of description.actions[name].links) {
if (link.rel === 'action') {
href = link.href;
break;
}
}
}

if (href) {
for (const name in description.actions) {
const action = description.actions[name];

let detail;
switch (action['@type']) {
case 'LockAction':
detail = new LockActionDetail(this, name, action, href);
break;
case 'UnlockAction':
detail = new UnlockActionDetail(this, name, action, href);
break;
default:
detail = new ActionDetail(this, name, action, href);
break;
}
if (!href) {
continue;
}

this.displayedActions[name] = { detail };
let detail;
switch (action['@type']) {
case 'LockAction':
detail = new LockActionDetail(this, name, action, href);
break;
case 'UnlockAction':
detail = new UnlockActionDetail(this, name, action, href);
break;
default:
detail = new ActionDetail(this, name, action, href);
break;
}
this.displayedActions[name] = { detail };
}
}

Expand Down
9 changes: 1 addition & 8 deletions static/js/views/action-input-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,7 @@ class ActionInputForm {
}
}

let body;
if (input) {
body = { [this.name]: { input } };
} else {
body = { [this.name]: { input: {} } };
}

API.postJson(this.href, body)
API.postJson(this.href, input)
.then(() => {
window.history.back();
})
Expand Down
4 changes: 2 additions & 2 deletions static/js/views/things.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ const ThingsScreen = {
}

let href;
for (const link of description.links) {
if (link.rel === 'actions') {
for (const link of description.actions[actionName].links) {
if (link.rel === 'action') {
href = link.href;
break;
}
Expand Down

0 comments on commit e79ce4a

Please sign in to comment.