Skip to content

Commit

Permalink
Fix: App Startup Tests and Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
giquieu committed Aug 12, 2023
1 parent 6177e47 commit 8a571d1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
9 changes: 8 additions & 1 deletion __tests__/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { App } from '../../src/app'
import { Incoming } from '../../src/services/incoming'
import { getConfig } from '../../src/services/config'
import { Outgoing } from '../../src/services/outgoing'
import { Client, getClient } from '../../src/services/client'

const client = mock<Client>()

const getClientTest: getClient = async ({ phone, incoming, outgoing, getConfig, onNewLogin }) => {
return client
}

describe('index routes', () => {
test('ping', async () => {
const incoming = mock<Incoming>()
const outgoing = mock<Outgoing>()
const getConfig = mock<getConfig>()
const app: App = new App(incoming, outgoing, '', getConfig)
const app: App = new App(incoming, outgoing, '', getConfig, getClientTest)
const res = await request(app.server).get('/ping')
expect(res.text).toEqual('pong!')
})
Expand Down
10 changes: 9 additions & 1 deletion __tests__/routes/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import { writeFileSync, existsSync, mkdirSync } from 'fs'
import { Outgoing } from '../../src/services/outgoing'
import { getMediaStore, MediaStore } from '../../src/services/media_store'
import { getStore, Store } from '../../src/services/store'
import { Client, getClient } from '../../src/services/client'

const phone = `${new Date().getTime()}`
const messageId = `wa.${new Date().getTime()}`
const url = `http://somehost`
const mimetype = 'text/plain'
const extension = 'txt'

const client = mock<Client>()
const dataStore = mock<DataStore>()
const store = mock<Store>()

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getTestDataStore: getDataStore = (_phone: string, _config: unknown): DataStore => {
return dataStore
Expand All @@ -37,6 +42,9 @@ const getConfigTest: getConfig = async (_phone: string) => {
defaultConfig.getStore = getTestStore
return defaultConfig
}
const getClientTest: getClient = async ({ phone, incoming, outgoing, getConfig, onNewLogin }) => {
return client
}

describe('media routes', () => {
let incoming: Incoming
Expand All @@ -46,7 +54,7 @@ describe('media routes', () => {
beforeEach(() => {
incoming = mock<Incoming>()
outgoing = mock<Outgoing>()
app = new App(incoming, outgoing, url, getConfigTest)
app = new App(incoming, outgoing, url, getConfigTest, getClientTest)
})

test('index', async () => {
Expand Down
9 changes: 7 additions & 2 deletions __tests__/routes/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { Outgoing } from '../../src/services/outgoing'
import { defaultConfig, getConfig } from '../../src/services/config'
import { Response } from '../../src/services/response'
import { getStore, Store } from '../../src/services/store'
import { Client, getClient } from '../../src/services/client'

const client = mock<Client>()
const store = mock<Store>()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getConfigTest: getConfig = async (_phone: string) => {
Expand All @@ -18,6 +20,9 @@ const getConfigTest: getConfig = async (_phone: string) => {
const getTestStore: getStore = async (_phone: string, _config: object) => {
return store
}
const getClientTest: getClient = async ({ phone, incoming, outgoing, getConfig, onNewLogin }) => {
return client
}

let phone: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -29,10 +34,10 @@ let outgoing: Outgoing
describe('messages routes', () => {
beforeEach(() => {
phone = `${new Date().getTime()}`
outgoing = mock<Outgoing>()
json = { data: `${new Date().getTime()}` }
outgoing = mock<Outgoing>()
incoming = mock<Incoming>()
app = new App(incoming, outgoing, '', getConfigTest)
app = new App(incoming, outgoing, '', getConfigTest, getClientTest)
})

test('whatsapp with sucess', async () => {
Expand Down
8 changes: 7 additions & 1 deletion __tests__/routes/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import { Outgoing } from '../../src/services/outgoing'
import { getStore, Store } from '../../src/services/store'
import { Config, getConfig } from '../../src/services/config'
import { DataStore } from '../../src/services/data_store'
import { Client, getClient } from '../../src/services/client'

const client = mock<Client>()
const store = mock<Store>()
const config = mock<Config>()
const dataStore = mock<DataStore>()
const getConfig = mock<getConfig>()

const loadTemplates = jest.spyOn(dataStore, 'loadTemplates')
loadTemplates.mockResolvedValue([])
const getStore = jest.spyOn(config, 'getStore')
Expand All @@ -21,12 +24,15 @@ store.dataStore = dataStore
const getConfigTest: getConfig = async (_phone: string) => {
return config
}
const getClientTest: getClient = async ({ phone, incoming, outgoing, getConfig, onNewLogin }) => {
return client
}

describe('templates routes', () => {
test('index', async () => {
const incoming = mock<Incoming>()
const outgoing = mock<Outgoing>()
const app: App = new App(incoming, outgoing, '', getConfigTest)
const app: App = new App(incoming, outgoing, '', getConfigTest, getClientTest)
const res = await request(app.server).get('/v15.0/123/message_templates')
expect(res.status).toEqual(200)
})
Expand Down
8 changes: 7 additions & 1 deletion __tests__/routes/webook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import { App } from '../../src/app'
import { Incoming } from '../../src/services/incoming'
import { Outgoing } from '../../src/services/outgoing'
import { defaultConfig, getConfig } from '../../src/services/config'
import { Client, getClient } from '../../src/services/client'

const client = mock<Client>()

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getConfigTest: getConfig = async (_phone: string) => {
return defaultConfig
}
const getClientTest: getClient = async ({ phone, incoming, outgoing, getConfig, onNewLogin }) => {
return client
}

describe('webhook routes', () => {
test('whatsapp', async () => {
const incoming = mock<Incoming>()
const outgoing = mock<Outgoing>()
const app: App = new App(incoming, outgoing, '', getConfigTest)
const app: App = new App(incoming, outgoing, '', getConfigTest, getClientTest)
const res = await request(app.server).post('/webhooks/whatsapp/123')
expect(res.status).toEqual(200)
})
Expand Down
2 changes: 1 addition & 1 deletion src/services/client_baileys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class ClientBaileys implements Client {
onStatus: this.onStatus,
onNewLogin: this.onNewLogin,
config: this.config,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
onDisconnected: async (_phone: string, _payload: any) => this.disconnect(),
onReconnect: this.onReconnect,
})
Expand Down
31 changes: 15 additions & 16 deletions src/services/new_login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import { Outgoing } from './outgoing'
import { OnNewLogin } from './socket'
import { phoneNumberToJid } from './transformer'

export const onNewLogin =
(outgoing: Outgoing): OnNewLogin =>
(phone: string): Promise<void> => {
const message = `Please be careful, the http endpoint is unprotected and if it is exposed in the network, someone else can send message as you!`
const payload = {
key: {
remoteJid: phoneNumberToJid(phone),
id: uuid(),
},
message: {
conversation: message,
},
messageTimestamp: new Date().getTime(),
}
return outgoing.sendOne(phone, payload)
// eslint-disable-next-line
export const onNewLogin = (outgoing: Outgoing): OnNewLogin =>
(phone: string): Promise<void> => {
const message = `Please be careful, the http endpoint is unprotected and if it is exposed in the network, someone else can send message as you!`
const payload = {
key: {
remoteJid: phoneNumberToJid(phone),
id: uuid(),
},
message: {
conversation: message,
},
messageTimestamp: new Date().getTime(),
}

return outgoing.sendOne(phone, payload)
}

0 comments on commit 8a571d1

Please sign in to comment.