• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / fiware-pep-steelskin / 11256688872

09 Oct 2024 02:06PM CUT coverage: 78.988% (-3.8%) from 82.787%
11256688872

Pull #544

github

web-flow
Merge 8bdd7b564 into 2c45e28dd
Pull Request #544: Task/add access matches (about users, headers, path. .. ) to access Logger

266 of 378 branches covered (70.37%)

Branch coverage included in aggregate %.

19 of 57 new or added lines in 4 files covered. (33.33%)

20 existing lines in 2 files now uncovered.

764 of 926 relevant lines covered (82.51%)

50.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.76
/lib/plugins/orionPlugin.js
1
/*
2
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
3
 *
4
 * This file is part of fiware-pep-steelskin
5
 *
6
 * fiware-pep-steelskin is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU Affero General Public License as
8
 * published by the Free Software Foundation, either version 3 of the License,
9
 * or (at your option) any later version.
10
 *
11
 * fiware-pep-steelskin is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public
17
 * License along with fiware-pep-steelskin.
18
 * If not, seehttp://www.gnu.org/licenses/.
19
 *
20
 * For those usages not covered by the GNU Affero General Public License
21
 * please contact with::[iot_support@tid.es]
22
 */
23

24
'use strict';
25

26
var errors = require('../errors'),
1✔
27
    identificationTable = require('./orionUrls');
28

29

30
/**
31
 * Translates the updateAction value to the appropriate action name for the Access Control.
32
 *
33
 * @param {Object} logger                Instance of logops.Logger
34
 * @param {String} originalAction        String with the action name.
35
 * @return {String}                      The string representation of the action name for the Access Control.
36
 */
37
function translateAction(logger, originalAction) {
1✔
38
    var action;
88✔
39

40
    switch (originalAction.toLowerCase()) {
88!
41
        case 'append':
42
            action = 'create';
80✔
43
            break;
80✔
44
            
45
        case 'appendstrict':
46
            action = 'create';
2✔
47
            break;
2✔
48

49
        case 'update':
50
            action = 'update';
2✔
51
            break;
2✔
52

53
        case 'delete':
54
            action = 'delete';
2✔
55
            break;
2✔
56

57
        case 'replace':
58
            action = 'update';
2✔
59
            break;
2✔
60

61
        default:
62
            action = null;
×
63
    }
64

65
    logger.debug('Discovered action was: %s', action);
88✔
66

67
    return action;
88✔
68
}
69

70
/**
71
 * Extract the action from a JSON body.
72
 *
73
 * @param {Object} logger        Instance of logops.Logger
74
 * @param {Object} body          Javascript Object with the parsed payload.
75
 * @param {Object} field         Field that will be used to extract the type.
76
 */
77
function inspectBodyJSON(logger, body, field, callback) {
1✔
78
    if (body && body[field]) {
88!
79
        var translatedAction = translateAction(logger, body[field]);
88✔
80

81
        if (translatedAction) {
88!
82
            callback(null, translatedAction);
88✔
83
        } else {
84
            callback(new errors.WrongJsonPayload());
×
85
        }
86
    } else {
87
        logger.error('[ORION-PLUGIN-001] Wrong JSON Payload: updateAction element not found');
×
88

89
        callback(new errors.WrongJsonPayload());
×
90
    }
91
}
92

93
function inspectBodyV2(req, res, callback) {
1✔
94
    const logger = req.logger;
88✔
95
    var actionHandler = function actionHandler(error, action) {
88✔
96
        req.action = action;
88✔
97
        callback(error, req, res);
88✔
98
    };
99

100
    if (req.is('*/json')) {
88!
101
        logger.debug('Inspecting JSON body to discover action: \n%j\n\n', req.body);
88✔
102
        inspectBodyJSON(logger, req.body, 'actionType', actionHandler);
88✔
103

104
    } else {
105
        // TODO: raise error if the type is not recognized.
106
        logger.error('[ORION-PLUGIN-002] Unknown content type: %s', req.headers['content-type']);
×
107

108
        actionHandler(new errors.UnexpectedContentType(req.headers['content-type']));
×
109
    }
110
}
111

112
/**
113
 * Determines what is the requested action based on the request information, knowing that it is a convenience operation.
114
 *
115
 * @param {Object} req           Incoming request.
116
 * @param {Object} res           Outgoing response.
117
 */
118
function inspectUrl(req, res, callback) {
1✔
119
    const logger = req.logger;
32✔
120
    var match = false;
32✔
121

122
    logger.debug('Extracting action from the URL "%s"', req.url);
32✔
123

124
    for (var i = 0; i < identificationTable.length; i++) {
32✔
125
        match = false;
308✔
126

127
        if (req.method === identificationTable[i][0] &&
308✔
128
            req.path.toLowerCase().match(identificationTable[i][1])) {
129

130
            match = true;
32✔
131

132
            if (identificationTable[i].length >= 4 && req.query) {
32✔
133
                for (var j in identificationTable[i][3]) {
3✔
134
                    if (!req.query[j] ||
3✔
135
                        identificationTable[i][3].hasOwnProperty(j) &&
136
                        req.query[j].split(',').indexOf(identificationTable[i][3][j]) < 0) {
137
                        match = false;
1✔
138
                    }
139
                }
140
            }
141

142
            if (match) {
32✔
143
                req.action = identificationTable[i][2];
31✔
144
                callback(null, req, res);
31✔
145
                return;
31✔
146
            }
147
        }
148
    }
149

150
    logger.error('[ORION-PLUGIN-003] Action not found');
1✔
151
    callback(new errors.ActionNotFound(), req, res);
1✔
152
}
153

154
/**
155
 * Middleware to calculate what Context Broker action has been received based on the path and the request payload.
156
 *
157
 * @param {Object} req           Incoming request.
158
 * @param {Object} res           Outgoing response.
159
 */
160
function extractCBAction(req, res, callback) {
1✔
161
    if (req.path.toLowerCase().match(/\/v2\/op\/update$/)) {
120✔
162
        inspectBodyV2(req, res, callback);
88✔
163
    } else {
164
        inspectUrl(req, res, callback);
32✔
165
    }
166
}
167

168
exports.extractCBAction = extractCBAction;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc