Skip to content

Commit

Permalink
feat: adds test account operation (#960)
Browse files Browse the repository at this point in the history
* feat: testAccount operation

* fix: npm install

* fix: dependency issue

* fix: lint errors

* fix: modified tests & lint errors

* fix: test  name

* fix: updated tests

* fix: updated tests
  • Loading branch information
sravya-yelleti authored Jul 20, 2022
1 parent 0f6d378 commit cd0d842
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 21 deletions.
22 changes: 22 additions & 0 deletions examples/crud-test-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { amazonAdvertising } from './auth'
import { AmazonMarketplaceAdvertisingCountryCode } from '@scaleleap/amazon-marketplaces'
import { CreateAccount } from '../src/operations/test-accounts/types'

const testAccountOperation = amazonAdvertising.testAccount
const REQUEST_ID = 'VMTZD2V14R745AHA5C4S'

// Retrieves a single testAccount specified by identifier.
testAccountOperation.getTestAccount(REQUEST_ID)

// Retrieves one or more testAccounts associated with the authorization passed in the request header.
testAccountOperation.listTestAccounts()

// Registers a brand in the sandbox environment.
const createTestAccount: CreateAccount = {
countryCode: AmazonMarketplaceAdvertisingCountryCode.IT,
accountType: 'VENDOR',
accountMetaData: {
vendorCode: 'ABCDE',
},
}
testAccountOperation.createTestAccount(createTestAccount)
21 changes: 0 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/amazon-advertising.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { SponsoredDisplayNegativeTargetingOperation } from './operations/negativ
import { SponsoredProductsNegativeTargetingOperation } from './operations/negative-targeting/sponsored-products-negative-targeting-operation'
import { SponsoredProductsProductTargetingOperation } from './operations/product-targeting/sponsored-products-product-targeting-operation'
import { ProfileOperation } from './operations/profiles/profile-operation'
import { TestAccountOperation } from './operations/test-accounts/test-account-operation'
import { SponsoredBrandsBidRecommendationsOperation } from './operations/recommendations/sponsored-brands-bid-recommendations-operation'
import { SponsoredBrandsTargetingRecommendationsOperation } from './operations/recommendations/sponsored-brands-targeting-recommendations-operation'
import { SponsoredBrandsReportOperation } from './operations/reports/sponsored-brands/sponsored-brands-report-operation'
Expand Down Expand Up @@ -167,6 +168,11 @@ export class AmazonAdvertising {
return this.operationProvider.create(ProfileOperation)
}

@LazyGetter()
get testAccount() {
return this.operationProvider.create(TestAccountOperation)
}

@LazyGetter()
get sponsoredBrandsBidRecommendations() {
return this.operationProvider.create(SponsoredBrandsBidRecommendationsOperation)
Expand Down
40 changes: 40 additions & 0 deletions src/operations/test-accounts/test-account-operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Operation } from '../operation'
import { Account, RequestId, CreateAccount, CreateAccountResponse } from './types'
import { Decode, DecodeArray } from '../../decorators'

export class TestAccountOperation extends Operation {
protected resource = 'testAccounts'

/**
* Retrieves one or more testAccounts associated with the authorization passed in the request header.
*
* @returns
*/
@DecodeArray(Account)
public listTestAccounts() {
return this.client.get<Account[]>(`${this.resource}`)
}

/**
* Retrieves a single account specified by identifier.
*
* @param requestId -
* @returns
*/
@Decode(Account)
public getTestAccount(requestId: RequestId) {
return this.client.get<Account>(`${this.resource}/?requestId=${requestId}`)
}

/**
* Create a test account
* @param createAccount
* @returns
*/
@Decode(CreateAccountResponse)
public createTestAccount(createAccount: CreateAccount) {
return this.client.post<CreateAccountResponse>(`${this.resource}`, {
createAccount,
})
}
}
58 changes: 58 additions & 0 deletions src/operations/test-accounts/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { isRight } from 'fp-ts/lib/Either'
import * as t from './types'

describe('CreateAccountResponse', () => {
it('should pass', () => {
const res = t.CreateAccountResponse.decode({
requestId: 'VMTZD2V14R745AHA5C4S',
})

expect(isRight(res)).toBeTruthy()
})

it('should fail', () => {
const res = t.CreateAccountResponse.decode({})

expect(isRight(res)).toBeFalsy()
})
})

describe('Account', () => {
it('should pass', () => {
const res = t.Account.decode({
countryCode: 'IT',
asins: [],
accountType: 'VENDOR',
id: 'ENTITY2TQYXTN0FH5DK',
status: 'COMPLETED',
})

expect(isRight(res)).toBeTruthy()
})

it('should fail', () => {
const res = t.Account.decode({})

expect(isRight(res)).toBeFalsy()
})
})

describe('CreateAccount', () => {
it('should pass', () => {
const res = t.CreateAccount.decode({
countryCode: 'IT',
accountMetaData: {
vendorCode: 'ABCDE',
},
accountType: 'VENDOR',
})

expect(isRight(res)).toBeTruthy()
})

it('should fail', () => {
const res = t.CreateAccount.decode({})

expect(isRight(res)).toBeFalsy()
})
})
57 changes: 57 additions & 0 deletions src/operations/test-accounts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as t from 'io-ts'
import { AmazonMarketplaceAdvertisingCountryCodeType } from '../commons/types'

export const RequestId = t.string
export type RequestId = t.TypeOf<typeof RequestId>

export const VendorCode = t.string
export type VendorCode = t.TypeOf<typeof VendorCode>

export const AccountMetaData = t.type({
vendorCode: VendorCode,
})
export type AccountMetaData = t.TypeOf<typeof AccountMetaData>

export const AccountInfoType = t.union([t.literal('AUTHOR'), t.literal('VENDOR')])
export type AccountInfoType = t.TypeOf<typeof AccountInfoType>

export const CreateAccount = t.intersection([
t.type({
/**
* The country code identifying the publisher(s) on which ads will run.
*/
countryCode: AmazonMarketplaceAdvertisingCountryCodeType,
/**
* Account info.
*/
accountType: AccountInfoType,
}),
t.partial({
accountMetaData: AccountMetaData,
}),
])
export type CreateAccount = t.TypeOf<typeof CreateAccount>

export const CreateAccountResponseStatus = t.union([
t.literal('IN_PROGRESS'),
t.literal('COMPLETED'),
t.literal('FAILED'),
])
export type CreateAccountResponseStatus = t.TypeOf<typeof CreateAccountResponseStatus>

export const CreateAccountResponse = t.type({
/**
* The RequestId of the account that was created
*/
requestId: RequestId,
})
export type CreateAccountResponse = t.TypeOf<typeof CreateAccountResponse>

export const Account = t.type({
countryCode: AmazonMarketplaceAdvertisingCountryCodeType,
asins: t.array(t.string),
accountType: AccountInfoType,
id: t.string,
status: CreateAccountResponseStatus,
})
export type Account = t.TypeOf<typeof Account>
6 changes: 6 additions & 0 deletions test/amazon-advertising.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SponsoredBrandsProductTargetingOperation } from '../src/operations/prod
import { SponsoredDisplayNegativeTargetingOperation } from '../src/operations/negative-targeting/sponsored-display-negative-targeting-operation'
import { SponsoredProductsProductTargetingOperation } from '../src/operations/product-targeting/sponsored-products-product-targeting-operation'
import { ProfileOperation } from '../src/operations/profiles/profile-operation'
import { TestAccountOperation } from '../src/operations/test-accounts/test-account-operation'
import { SponsoredBrandsBidRecommendationsOperation } from '../src/operations/recommendations/sponsored-brands-bid-recommendations-operation'
import { SponsoredBrandsTargetingRecommendationsOperation } from '../src/operations/recommendations/sponsored-brands-targeting-recommendations-operation'
import { SponsoredBrandsReportOperation } from '../src/operations/reports/sponsored-brands/sponsored-brands-report-operation'
Expand Down Expand Up @@ -147,6 +148,11 @@ describe('AmazonAdvertising', () => {
expect(operation).toBeInstanceOf(ProfileOperation)
})

it('should return TestAccountOperation', () => {
const operation = amazonAdvertising.testAccount
expect(operation).toBeInstanceOf(TestAccountOperation)
})

it('should return SponsoredBrandsBidRecommendationsOperation', () => {
const operation = amazonAdvertising.sponsoredBrandsBidRecommendations
expect(operation).toBeInstanceOf(SponsoredBrandsBidRecommendationsOperation)
Expand Down
51 changes: 51 additions & 0 deletions test/operations/test-accounts/test-accounts-operation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { OperationProvider } from '../../../src/operations/operation-provider'
import { TestAccountOperation } from '../../../src/operations/test-accounts/test-account-operation'
import { httpClientFactory } from '../../http-client-factory'
import { Account } from '../../../src/operations/test-accounts/types'
import { AmazonMarketplaceAdvertisingCountryCode } from '@scaleleap/amazon-marketplaces'
import { delay } from '../../test-utils'

jest.setTimeout(15000)

describe.skip('TestAccountOperation', () => {
const client = httpClientFactory()
const operationProvider = new OperationProvider(client)
const accountOperation = operationProvider.create(TestAccountOperation)
const REQUEST_ID = 'VMTZD2V14R745AHA5C4S'

describe('listAccounts', () => {
it(`should return an array of accounts`, async () => {
const res: Account[] = await accountOperation.listTestAccounts()

expect(Array.isArray(res)).toBeTruthy()
})
})

describe('getAccount', () => {
it(`should return a account object`, async () => {
const account = await accountOperation.getTestAccount(REQUEST_ID)

expect(account).toBeTruthy()
})
})

describe('createTestAccount', () => {
it(`should create the test account`, async () => {
const res = await accountOperation.createTestAccount({
countryCode: AmazonMarketplaceAdvertisingCountryCode.IT,
accountType: 'VENDOR',
accountMetaData: {
vendorCode: 'ABCDE',
},
})

expect(res).toBeTruthy()
expect(res).toHaveLength(1)

await delay()

const account = await accountOperation.getTestAccount(REQUEST_ID)
expect(account).toBeTruthy()
})
})
})

0 comments on commit cd0d842

Please sign in to comment.