Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

497 Allow passing credentials and token as objects #510

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// eslint-disable-next-line camelcase
import { check_inbox } from 'gmail-tester'
import type { Credentials } from 'gmail-tester'
import type { Services } from '@wdio/types'

import type { CheckInboxOptions, WdioGmailServiceOptions } from './types.js'

export default class GmailService implements Services.ServiceInstance {
private _credentialsJsonPath: string
private _tokenJsonPath: string
private _credentials: string | Credentials
private _token: string | Record<string, unknown>
private _intervalSec: number
private _timeoutSec: number
constructor (serviceOptions: WdioGmailServiceOptions) {
this._credentialsJsonPath = serviceOptions.credentialsJsonPath
this._tokenJsonPath = serviceOptions.tokenJsonPath
this._credentials = serviceOptions.credentials
this._token = serviceOptions.token
this._intervalSec = serviceOptions.intervalSec ?? 10
this._timeoutSec = serviceOptions.timeoutSec ?? 60
}
Expand All @@ -25,17 +26,17 @@ export default class GmailService implements Services.ServiceInstance {
throw new Error('At least one of `from`, `to` or `subject` need to be provided to checkInbox')
}

if (typeof this._credentialsJsonPath !== 'string') {
throw new Error('Service option "credentialsJsonPath" not set, but required')
if (!this._credentials) {
throw new Error('Service option "credentials" not set, but required')
}

if (typeof this._tokenJsonPath !== 'string') {
throw new Error('Service option "tokenJsonPath" not set, but required')
if (!this._token) {
throw new Error('Service option "token" not set, but required')
}

return await check_inbox(
this._credentialsJsonPath,
this._tokenJsonPath,
this._credentials,
this._token,
{
from,
to,
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Credentials } from 'gmail-tester'

export interface CheckInboxOptions {
includeBody?: boolean;
from?: string;
Expand All @@ -10,8 +12,8 @@ export interface CheckInboxOptions {
}

export interface WdioGmailServiceOptions {
credentialsJsonPath: string;
tokenJsonPath: string;
credentials: string | Credentials;
token: string | Record<string, unknown>;
intervalSec?: number;
timeoutSec?: number;
}
6 changes: 3 additions & 3 deletions tests/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('registers checkInbox command', () => {
test('throws if required options not set', async () => {
const service = new GmailService({} as any)
await expect(() => service['_checkInbox']({ from: 'foo' } as any)).rejects.toThrow(/not set, but required/)
service['_credentialsJsonPath'] = 'foo'
service['_credentials'] = 'foo'
await expect(() => service['_checkInbox']({ from: 'foo' } as any)).rejects.toThrow(/not set, but required/)
})

Expand All @@ -33,8 +33,8 @@ test('throws if from, to or subject is not set', async () => {

test('throws if from, to or subject is not set', async () => {
const service = new GmailService({
credentialsJsonPath: 'foo',
tokenJsonPath: 'bar'
credentials: 'foo',
token: 'bar'
} as any)

await service['_checkInbox']({ from: 'foo' })
Expand Down
Loading