Skip to Content
0
Nov 14, 2022 at 02:26 PM

Testing API with mocha - authorization issue [mocha]

106 Views Last edit Nov 14, 2022 at 03:37 PM 3 rev

Hello,

i would like to test one of the APIs in the business hub (https://api.sap.com/api/OP_API_MAINTNOTIFICATION/overview?ReleaseInfo=2021%20FPS02)

with mocha. In order to do it it is required to authorize yourself/generate an authorization token.

let chai = require("chai");
let chaiHttp = require("chai-http");
let server = require("./apiPage");

//Assertion Style
chai.should();
chai.use(chaiHttp);

describe("API TEST", function() {
    it.only('Get all notifications', function(done) {
        const url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_MAINTNOTIFICATION/MaintenanceNotification";
        chai.request(url)
            .post('/auth/sign_in')
            // send user login details
            .send({
                'email': 'ExampleEmail',
                'password': 'ExamplePassword'
            })
            .end(function(err, res) {
                var token = res.body.token;
                done();
            });

        chai.request(url)
            .get('')
            .set('Authorization', 'JWT ' + token)
            .end(function(err, res) {
                console.log(res);
                done();
            });
    });
});

Issue: It doesn't really function.

Question: How to authorize yourself/generate a token in order to test API: Maintenance Notification ?