Skip to content

Commit

Permalink
change axios post by get and modify test
Browse files Browse the repository at this point in the history
  • Loading branch information
William-De71 committed Nov 11, 2024
1 parent 8de5e0c commit 2065f9f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 62 deletions.
4 changes: 2 additions & 2 deletions server/services/free-mobile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ module.exports = function FreeMobileService(gladys, serviceId) {
async function send(message) {
const url = 'https://smsapi.free-mobile.fr/sendmsg';

const data = {
const params = {
user: username,
pass: accessToken,
msg: message,
};

try {
const response = await axios.post(url, data);
const response = await axios.get(url, { params });
logger.debug('SMS successfully sent:', response.data);
} catch (e) {
logger.error('Error sending SMS:', e);
Expand Down
140 changes: 80 additions & 60 deletions server/test/services/free-mobile/index.test.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,137 @@
const sinon = require('sinon');
const { expect } = require('chai');

const axios = require('axios');
const logger = require('../../../utils/logger');
const FreeMobileService = require('../../../services/free-mobile');
const assert = require('assert');
const proxyquire = require('proxyquire').noCallThru();
const { ServiceNotConfiguredError } = require('../../../utils/coreErrors');
const logger = require('../../../utils/logger');

const serviceId = 'f87b7af2-ca8e-44fc-b754-444354b42fee';

describe('free-mobile', () => {
describe('FreeMobileService', () => {
let FreeMobileService;
let axiosStub;
let gladys;
let freeMobileService;
let axiosPostStub;
let loggerErrorStub;

beforeEach(() => {
axiosStub = {
get: async () => {
return { data: 'OK' };
},
};

FreeMobileService = proxyquire('../../../services/free-mobile', {
axios: axiosStub,
});

gladys = {
variable: {
getValue: sinon.stub(),
getValue: async (key) => {
if (key === 'FREE_MOBILE_USERNAME') {
return 'validUsername';
}
if (key === 'FREE_MOBILE_ACCESS_TOKEN') {
return 'validAccessToken';
}
return null;
},
},
};
freeMobileService = FreeMobileService(gladys, serviceId);

axiosPostStub = sinon.stub(axios, 'post');
loggerErrorStub = sinon.stub(logger, 'error');
});

afterEach(() => {
axiosPostStub.restore();
loggerErrorStub.restore();
sinon.restore();
freeMobileService = FreeMobileService(gladys, serviceId);
});

describe('start', () => {
it('should start service with success', async () => {
await freeMobileService.start();

assert.strictEqual(await gladys.variable.getValue('FREE_MOBILE_USERNAME', serviceId), 'validUsername');
assert.strictEqual(await gladys.variable.getValue('FREE_MOBILE_ACCESS_TOKEN', serviceId), 'validAccessToken');
});

it('should throw ServiceNotConfiguredError if username is missing', async () => {
gladys.variable.getValue.resolves(null);
// gladys.variable.getValue.resolves(null);
gladys.variable.getValue = async (key) => {
if (key === 'FREE_MOBILE_USERNAME') {
return null;
}
if (key === 'FREE_MOBILE_ACCESS_TOKEN') {
return 'validAccessToken';
}
return null;
};

try {
await freeMobileService.start();
throw new Error('Expected ServiceNotConfiguredError to be thrown');
} catch (error) {
expect(error).to.be.instanceOf(ServiceNotConfiguredError);
} catch (e) {
expect(e).instanceOf(ServiceNotConfiguredError);
}
});

it('should throw ServiceNotConfiguredError if accessToken is missing', async () => {
/*
gladys.variable.getValue
.onFirstCall()
.resolves('validUsername')
.onSecondCall()
.resolves(null);

*/
gladys.variable.getValue = async (key) => {
if (key === 'FREE_MOBILE_USERNAME') {
return 'validUsername';
}
if (key === 'FREE_MOBILE_ACCESS_TOKEN') {
return null;
}
return null;
};

try {
await freeMobileService.start();
throw new Error('Expected ServiceNotConfiguredError to be thrown');
} catch (error) {
expect(error).to.be.instanceOf(ServiceNotConfiguredError);
} catch (e) {
expect(e).instanceOf(ServiceNotConfiguredError);
}
});

});

describe('send', () => {
it('should send SMS successfully', async () => {
gladys.variable.getValue
.onFirstCall()
.resolves('validUsername')
.onSecondCall()
.resolves('validAccessToken');

axiosPostStub.resolves({ data: 'success' });

beforeEach(async () => {
await freeMobileService.start();
});

logger.debug('username:', await gladys.variable.getValue.firstCall.returnValue);
logger.debug('accessToken:', await gladys.variable.getValue.secondCall.returnValue);

await freeMobileService.sms.send('Hello World');

expect(axiosPostStub.calledOnce).to.equal(true);

const callArgs = axiosPostStub.getCall(0).args;
logger.debug('Arguments de l’appel à axios.post:', callArgs);
expect(callArgs[0]).to.equal('https://smsapi.free-mobile.fr/sendmsg');
expect(callArgs[1]).to.deep.equal({
user: 'validUsername',
pass: 'validAccessToken',
msg: 'Hello World',
});
it('should send SMS successfully', async () => {
axiosStub.get = async (url, options) => {
assert.strictEqual(url, 'https://smsapi.free-mobile.fr/sendmsg');
assert.deepStrictEqual(options.params, {
user: 'validUsername',
pass: 'validAccessToken',
msg: 'Hello',
});
return { data: 'OK' };
};

await freeMobileService.sms.send('Hello');
});

it('should log an error if SMS fails', async () => {
gladys.variable.getValue
.onFirstCall()
.resolves('validUsername')
.onSecondCall()
.resolves('validAccessToken');

axiosPostStub.rejects(new Error('Network error'));

await freeMobileService.start();
axiosStub.get = async () => {
throw new Error('Network error');
};
const loggerErrorStub = sinon.stub(logger, 'error');
await freeMobileService.sms.send('Hello World');

expect(loggerErrorStub.calledOnce).to.equal(true);

const errorArgs = loggerErrorStub.getCall(0).args;
expect(errorArgs[0]).to.equal('Error sending SMS:');
expect(errorArgs[1]).to.be.instanceOf(Error);
});

});

describe('stop', () => {
it('should stopping service', async () => {
await freeMobileService.stop();
});
});
});
});

0 comments on commit 2065f9f

Please sign in to comment.