Skip to content

Commit

Permalink
Improving tests and now we are managing exceptions like network error…
Browse files Browse the repository at this point in the history
…s getting the notification content
  • Loading branch information
danilat committed Apr 14, 2020
1 parent 218e144 commit fda830e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
32 changes: 19 additions & 13 deletions sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ async function sendMessage(){
const token = core.getInput("token");
const to = core.getInput("to");
const parse_mode = core.getInput("parse-mode");
let response = await httpClient.get(contentUrl)
let message = encodeURI(await response.readBody())
console.log("Message to send to telegram:", message)
console.log("Sending message to send:", to)
const telegramEndopoint = `https://api.telegram.org/bot${token}/sendMessage?chat_id=${to}&text=${message}&parse_mode=${parse_mode}`
const telegramResponse = await httpClient.post(telegramEndopoint)
const telegramMessage = await telegramResponse.readBody()
console.log("Telegrams response:", telegramMessage)
if (telegramResponse.message.statusCode != 200) {
core.setFailed("Telegram FAILED", telegramMessage);
} else {
core.setOutput("Telegrams SUCCESS", telegramMessage);

try {
let response = await httpClient.get(contentUrl)

let message = encodeURI(await response.readBody())
console.log("Message to send to telegram:", message)
console.log("Sending message to send:", to)
const telegramEndopoint = `https://api.telegram.org/bot${token}/sendMessage?chat_id=${to}&text=${message}&parse_mode=${parse_mode}`
const telegramResponse = await httpClient.post(telegramEndopoint)
const telegramMessage = await telegramResponse.readBody()
console.log("Telegrams response:", telegramMessage)
if (telegramResponse.message.statusCode != 200) {
core.setFailed("Telegram FAILED", telegramMessage);
} else {
core.setOutput("Telegrams SUCCESS", telegramMessage);
}
return telegramResponse;
} catch(error){
core.setFailed("Some error happened", error);
}
return telegramResponse;

}

Expand Down
77 changes: 61 additions & 16 deletions tests/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,65 @@ const core = require("@actions/core");
const sendMessage = require("../sendMessage");
require("dotenv").config()

describe("run the action", () =>{
let failedSpy;
beforeEach(() => {
core.getInput = (argName) => {
switch (argName) {
case "to":
return process.env.TO;
case "content-url":
return process.env.URL;
case "token":
return process.env.TOKEN;
case "parse-mode":
return "html";
}
};
failedSpy = jest.spyOn(core, "setFailed");
});

test("run the action", async () => {
core.getInput = (argName) => {
switch (argName) {
case "to":
return process.env.TO;
case "content-url":
return process.env.URL;
case "token":
return process.env.TOKEN;
case "parse-mode":
return "html";
}
};
const reponse = await sendMessage()
expect(reponse.message.statusCode).toBe(200);
});
afterEach(() => {
jest.clearAllMocks();
});

describe("when the configuration is correct", () =>{
test("the notification is sent", async () => {
const reponse = await sendMessage()

expect(reponse.message.statusCode).toBe(200);
});
});

describe("when the TOKEN configuration is incorrect", () =>{
test("the notification is not sent", async () => {
process.env.TOKEN = "foo"

const reponse = await sendMessage()

expect(reponse.message.statusCode).toBe(404);
expect(failedSpy).toHaveBeenCalled();
});
});

describe("when the TO configuration is incorrect", () =>{
test("the notification is not sent", async () => {
process.env.TO = "foo"

const reponse = await sendMessage()

expect(reponse.message.statusCode).toBe(404);
expect(failedSpy).toHaveBeenCalled();
});
});

describe("when the URL configuration is incorrect", () =>{
test("the notification is not sent", async () => {
process.env.URL = "blabla"

const reponse = await sendMessage()

expect(failedSpy).toHaveBeenCalled();
});
});
})

0 comments on commit fda830e

Please sign in to comment.