Skip to content

Commit

Permalink
add test for receive code meassage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Nov 10, 2023
1 parent 7558adf commit 688adba
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
5 changes: 5 additions & 0 deletions adapters/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export declare class Captcha {
* retruns OTP-adapter
*/
export declare class OTP {
/**
* @deprecated use Adapter.getOTPAdapter instead
* @param adapterName
*/
static getAdapter(adapterName?: string): Promise<OTPAdapter>;
}
/** TODO: move other Adapters to one class adapter */
Expand All @@ -26,6 +30,7 @@ export declare class Adapter {
private static instanceDeliveryAdapter;
private static instanceMF;
static WEBRESTO_MODULES_PATH: string;
static getOTPAdapter(adapterName?: string): Promise<OTPAdapter>;
static getPromotionAdapter(adapter?: string | PromotionAdapter, initParams?: {
[key: string]: string | number | boolean;
}): PromotionAdapter;
Expand Down
15 changes: 11 additions & 4 deletions adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ exports.Captcha = Captcha;
* retruns OTP-adapter
*/
class OTP {
/**
* @deprecated use Adapter.getOTPAdapter instead
* @param adapterName
*/
static async getAdapter(adapterName) {
return Adapter.getOTPAdapter(adapterName);
}
}
exports.OTP = OTP;
/** TODO: move other Adapters to one class adapter */
class Adapter {
static async getOTPAdapter(adapterName) {
if (!adapterName) {
adapterName = (await Settings.get("DEFAULT_OTP_ADAPTER"));
}
Expand All @@ -87,10 +98,6 @@ class OTP {
throw new Error("Module " + adapterLocation + " not found");
}
}
}
exports.OTP = OTP;
/** TODO: move other Adapters to one class adapter */
class Adapter {
static getPromotionAdapter(adapter, initParams) {
let adapterName;
if (adapter) {
Expand Down
31 changes: 20 additions & 11 deletions adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,26 @@ export class Captcha {
* retruns OTP-adapter
*/
export class OTP {
/**
* @deprecated use Adapter.getOTPAdapter instead
* @param adapterName
*/
public static async getAdapter(adapterName?: string): Promise<OTPAdapter> {
return Adapter.getOTPAdapter(adapterName);
}
}

/** TODO: move other Adapters to one class adapter */
export class Adapter {
// Singletons
private static instanceRMS: RMSAdapter;
private static instancePromotionAdapter: PromotionAdapter;
private static instanceDeliveryAdapter: DeliveryAdapter;
private static instanceMF: MediaFileAdapter;

public static WEBRESTO_MODULES_PATH = process.env.WEBRESTO_MODULES_PATH === undefined ? "@webresto" : process.env.WEBRESTO_MODULES_PATH;

public static async getOTPAdapter(adapterName?: string): Promise<OTPAdapter> {
if (!adapterName) {
adapterName = (await Settings.get("DEFAULT_OTP_ADAPTER")) as string;
}
Expand All @@ -70,17 +89,7 @@ export class OTP {
throw new Error("Module " + adapterLocation + " not found");
}
}
}

/** TODO: move other Adapters to one class adapter */
export class Adapter {
// Singletons
private static instanceRMS: RMSAdapter;
private static instancePromotionAdapter: PromotionAdapter;
private static instanceDeliveryAdapter: DeliveryAdapter;
private static instanceMF: MediaFileAdapter;

public static WEBRESTO_MODULES_PATH = process.env.WEBRESTO_MODULES_PATH === undefined ? "@webresto" : process.env.WEBRESTO_MODULES_PATH;

public static getPromotionAdapter(adapter?: string | PromotionAdapter, initParams?: {[key: string]:string | number | boolean}): PromotionAdapter {

let adapterName: string;
Expand Down
11 changes: 7 additions & 4 deletions libs/NotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ class NotificationManager {
static async sendMessageToUser(badge, text, user, type, subject, data) {
let populatedUser;
if (typeof user === "string") {
populatedUser = await User.find({ where: { or: [{ id: user }, { login: user }] } }).populate('devices');
const populatedUsers = await User.find({ where: { or: [{ id: user }, { login: user }] } }).populate('devices');
if (populatedUsers.length === 1) {
populatedUser = populatedUsers[0];
}
else {
throw new Error(`User not found`);
}
}
else {
populatedUser = user;
}
if (populatedUser.length !== 1) {
throw new Error(`User not found`);
}
await NotificationManager.send(badge, "user", text, populatedUser, type, subject, data);
}
}
Expand Down
11 changes: 6 additions & 5 deletions libs/NotificationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ export class NotificationManager {
let populatedUser;

if(typeof user === "string") {
populatedUser = await User.find({ where: { or: [{ id: user }, { login: user }] }}).populate('devices');
const populatedUsers = await User.find({ where: { or: [{ id: user }, { login: user }] }}).populate('devices');
if (populatedUsers.length === 1) {
populatedUser = populatedUsers[0];
} else {
throw new Error(`User not found`);
}
} else {
populatedUser = user;
}

if (populatedUser.length !== 1) {
throw new Error(`User not found`);
}

await NotificationManager.send(badge, "user", text, populatedUser, type, subject, data);
}
Expand Down
7 changes: 7 additions & 0 deletions test/integration/notification_manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ describe("NotificationManager", function () {
if (!result)
throw `Not exist??`;
});
it("OTP recive to user", async () => {
const otpAdapter = await Adapter.getOTPAdapter();
await otpAdapter.get("1123");
var match = testChannel.lastMessage.match(/Your code is (\d+)/);
if (!match)
throw `Not match with "Your code is ..."`;
});
});
7 changes: 7 additions & 0 deletions test/integration/notification_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ describe("NotificationManager", function () {
let result = NotificationManager.isChannelExist('sms');
if(!result) throw `Not exist??`
});

it("OTP recive to user", async () => {
const otpAdapter = await Adapter.getOTPAdapter();
await otpAdapter.get("1123");
var match = testChannel.lastMessage.match(/Your code is (\d+)/);
if(!match) throw `Not match with "Your code is ..."`
});
});

0 comments on commit 688adba

Please sign in to comment.