liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const should = require("should");
const request = require('supertest');
const express = require('express');
const bodyParser = require("body-parser");
const sinon = require('sinon');
 
let app;
 
const NR_TEST_UTILS = require("nr-test-utils");
const diagnostics = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/diagnostics");
 
describe("api/editor/diagnostics", function() {
    before(function() {
        app = express();
        app.use(bodyParser.json());
        app.get("/diagnostics",diagnostics.getReport);
    });
 
    it('returns the diagnostics report when explicitly enabled', function(done) {
        const settings = { diagnostics: { ui: true, enabled: true } }
        const runtimeAPI  = {
            diagnostics: {
                get: async function (opts) {
                    return new Promise(function (resolve, reject) {
                        opts = opts || {}
                        try {
                            resolve({ opts: opts, a:1, b:2});
                        } catch (error) {
                            error.status = 500;
                            reject(error);
                        }
                    })
                }
            }
        }
 
        diagnostics.init(settings, runtimeAPI);
 
        request(app)
        .get("/diagnostics")
        .expect(200)
        .end(function(err,res) {
            if (err || typeof res.error === "object") {
                return done(err || res.error);
            }
            res.should.have.property("statusCode",200);
            res.body.should.have.property("a",1);
            res.body.should.have.property("b",2);
            done();
        });
    });
    it('returns the diagnostics report when not explicitly enabled (implicitly enabled)', function(done) {
        const settings = { diagnostics: { enabled: undefined } }
        const runtimeAPI  = {
            diagnostics: {
                get: async function (opts) {
                    return new Promise(function (resolve, reject) {
                        opts = opts || {}
                        try {
                            resolve({ opts: opts, a:3, b:4});
                        } catch (error) {
                            error.status = 500;
                            reject(error);
                        }
                    })
                }
            }
        }
 
        diagnostics.init(settings, runtimeAPI);
 
        request(app)
        .get("/diagnostics")
        .expect(200)
        .end(function(err,res) {
            if (err || typeof res.error === "object") {
                return done(err || res.error);
            }
            res.should.have.property("statusCode",200);
            res.body.should.have.property("a",3);
            res.body.should.have.property("b",4);
            done();
        });
    });
    it('should error when setting is disabled', function(done) {
        const settings = { diagnostics: { ui: true, enabled: false } }
        const runtimeAPI  = {
            diagnostics: {
                get: async function (opts) {
                    return new Promise(function (resolve, reject) {
                        opts = opts || {}
                        try {
                            resolve({ opts: opts});
                        } catch (error) {
                            error.status = 500;
                            reject(error);
                        }
                    })
                }
            }
        }
 
        diagnostics.init(settings, runtimeAPI);
 
        request(app)
        .get("/diagnostics")
        .expect(403)
        .end(function(err,res) {
            if (!err && typeof res.error !== "object") {
                return done(new Error("accessing diagnostics endpoint while disabled should raise error"));
            }
            res.should.have.property("statusCode",403);
            res.body.should.have.property("message","diagnostics are disabled");
            res.body.should.have.property("code","diagnostics.disabled");
            done();
        });
    });
 
});