Skip to content

Commit

Permalink
Some Enhancement (line#162)
Browse files Browse the repository at this point in the history
* misc: remove polyfill for Nodejs version below 6

* refact: change NumberOfSentBroadcastMessages to NumberOfMessagesSentResponse

* add test for uncovered lines

* add test when set non-Buffer to setRichMenu

* remove no meaning try catch
  • Loading branch information
xingoxu authored Jul 31, 2019
1 parent 7f9b9de commit 8792d26
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/api-reference/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Client {
getNumberOfSentMulticastMessages(date: string): Promise<NumberOfMessagesSentResponse>
getTargetLimitForAdditionalMessages(): Promise<TargetLimitForAdditionalMessages>
getNumberOfMessagesSentThisMonth(): Promise<NumberOfMessagesSentThisMonth>
getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfSentBroadcastMessages>
getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfMessagesSentResponse>
}
```

Expand Down Expand Up @@ -485,7 +485,7 @@ client.getNumberOfMessagesSentThisMonth().then((response) => {
})
```

#### `getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfSentBroadcastMessages>`
#### `getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfMessagesSentResponse>`

Gets the number of messages sent with the `/bot/message/broadcast` endpoint.

Expand Down
4 changes: 2 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ export default class Client {

public async getNumberOfSentBroadcastMessages(
date: string,
): Promise<Types.NumberOfSentBroadcastMessages> {
const res = await this.http.get<Types.NumberOfSentBroadcastMessages>(
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/broadcast?date=${date}`,
);
return ensureJSON(res);
Expand Down
17 changes: 0 additions & 17 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1784,23 +1784,6 @@ export type NumberOfMessagesSentThisMonth = {
totalUsage: number;
};

export type NumberOfSentBroadcastMessages = {
/**
* Status of the counting process. One of the following values is returned:
* - `ready`: You can get the number of messages.
* - `unready`: The message counting process for the date specified in date has not been completed yet.
* Retry your request later. Normally, the counting process is completed within the next day.
* - `out_of_service`: The date specified in date is earlier than March 31, 2018,
* when the operation of the counting system started.
*/
status: "ready" | "unready" | "out_of_service";
/**
* The number of messages sent with the Messaging API on the date specified in `date`.
* The response has this property only when the value of `status` is `ready`.
*/
success?: number;
};

export const LINE_REQUEST_ID_HTTP_HEADER_NAME = "x-line-request-id";
export type MessageAPIResponseBase = {
[LINE_REQUEST_ID_HTTP_HEADER_NAME]?: string;
Expand Down
9 changes: 1 addition & 8 deletions lib/validate-signature.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { createHmac, timingSafeEqual } from "crypto";

function s2b(str: string, encoding: string): Buffer {
try {
return Buffer.from(str, encoding);
} catch (err) {
if (err.name === "TypeError") {
return new Buffer(str, encoding);
}
throw err;
}
return Buffer.from(str, encoding);
}

function safeCompare(a: Buffer, b: Buffer): boolean {
Expand Down
21 changes: 19 additions & 2 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "fs";
import { join } from "path";
import { deepEqual, equal } from "assert";
import { deepEqual, equal, ok } from "assert";
import Client from "../lib/client";
import * as Types from "../lib/types";
import { getStreamData } from "./helpers/stream";
Expand All @@ -10,7 +10,6 @@ const TEST_PORT = parseInt(process.env.TEST_PORT, 10);

const client = new Client({
channelAccessToken: "test_channel_access_token",
channelSecret: "test_channel_secret",
});

const getRecentReq = (): any =>
Expand Down Expand Up @@ -397,4 +396,22 @@ describe("client", () => {
equal(req.query.date, date);
equal(req.method, "GET");
});

it("fails on construct with no channelAccessToken", () => {
try {
new Client({ channelAccessToken: null });
ok(false);
} catch (err) {
equal(err.message, "no channel access token");
}
});

it("fails on pass non-Buffer to setRichMenu", async () => {
try {
await client.setRichMenuImage("test_rich_menu_id", null);
ok(false);
} catch (err) {
equal(err.message, "invalid data type for postBinary");
}
});
});
27 changes: 27 additions & 0 deletions test/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ describe("middleware", () => {
deepEqual(req.body.events, [webhook]);
});

it("fails on construct with no channelSecret", () => {
try {
middleware({ channelSecret: null });
ok(false);
} catch (err) {
equal(err.message, "no channel secret");
}
});

it("fails on wrong signature", async () => {
try {
await http({
Expand All @@ -101,6 +110,24 @@ describe("middleware", () => {
}
});

it("fails on wrong signature (length)", async () => {
try {
await http({
"X-Line-Signature": "WqJD7WAIZ6plkQGkj48w=",
}).post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 401);
} else {
throw err;
}
}
});

it("fails on invalid JSON", async () => {
try {
await http({
Expand Down

0 comments on commit 8792d26

Please sign in to comment.