-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: pull request #30 from railsware/general-api.
General api
- Loading branch information
Showing
18 changed files
with
677 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { MailtrapClient } from "../../src" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const accountAccessesClient = client.general.accountAccesses | ||
|
||
accountAccessesClient.listAccountAccesses() | ||
.then((result) => { | ||
const firstResult = result[0] | ||
|
||
return accountAccessesClient.removeAccountAccess(firstResult.id) | ||
}) | ||
.catch(console.error) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MailtrapClient } from "../../src" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const accountsClient = client.general.accounts | ||
|
||
accountsClient.getAllAcounts() | ||
.then(console.log) | ||
.catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { MailtrapClient } from "../../src" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const permissionsClient = client.general.permissions | ||
|
||
permissionsClient.getResources() | ||
.then((result) => { | ||
const firstResult = result[0] | ||
console.log(firstResult) | ||
|
||
return permissionsClient.bulkPermissionsUpdate(5142, [ | ||
{resourceId: '3281', resourceType: 'account', accessLevel: 'viewer'}, | ||
{resourceId: '3809', resourceType: 'inbox', destroy: 'true'} | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import axios from "axios"; | ||
|
||
import General from "../../../lib/api/General"; | ||
|
||
describe("lib/api/General: ", () => { | ||
const testInboxId = 100; | ||
const generalAPI = new General(axios, testInboxId); | ||
|
||
describe("class General(): ", () => { | ||
describe("init: ", () => { | ||
it("initalizes with all necessary params.", () => { | ||
expect(generalAPI).toHaveProperty("accountAccesses"); | ||
expect(generalAPI).toHaveProperty("accounts"); | ||
expect(generalAPI).toHaveProperty("permissions"); | ||
}); | ||
}); | ||
}); | ||
}); |
154 changes: 154 additions & 0 deletions
154
src/__tests__/lib/api/resources/AccountAccesses.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import axios from "axios"; | ||
import AxiosMockAdapter from "axios-mock-adapter"; | ||
|
||
import AccountAccesses from "../../../../lib/api/resources/AccountAccesses"; | ||
import handleSendingError from "../../../../lib/axios-logger"; | ||
import MailtrapError from "../../../../lib/MailtrapError"; | ||
|
||
import CONFIG from "../../../../config"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
describe("lib/api/resources/AccountAccesses: ", () => { | ||
let mock: AxiosMockAdapter; | ||
const accountId = 100; | ||
const accountAccessesAPI = new AccountAccesses(axios, accountId); | ||
const responseData = [ | ||
{ | ||
id: 42, | ||
specifier_type: "User", | ||
specifier: { | ||
id: 0, | ||
email: "user@example.com", | ||
name: "string", | ||
}, | ||
resources: [ | ||
{ | ||
resource_id: 0, | ||
resource_type: "account", | ||
access_level: 100, | ||
}, | ||
], | ||
permissions: { | ||
can_read: true, | ||
can_update: true, | ||
can_destroy: true, | ||
can_leave: true, | ||
}, | ||
}, | ||
]; | ||
const accountAccessId = 100; | ||
|
||
describe("class AccountAccesses(): ", () => { | ||
describe("init: ", () => { | ||
it("initalizes with all necessary params.", () => { | ||
expect(accountAccessesAPI).toHaveProperty("listAccountAccesses"); | ||
expect(accountAccessesAPI).toHaveProperty("removeAccountAccess"); | ||
}); | ||
}); | ||
}); | ||
|
||
beforeAll(() => { | ||
/** | ||
* Init Axios interceptors for handling response.data, errors. | ||
*/ | ||
axios.interceptors.response.use( | ||
(response) => response.data, | ||
handleSendingError | ||
); | ||
mock = new AxiosMockAdapter(axios); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
describe("listAccountAccesses(): ", () => { | ||
it("successfully gets list of user and invite account accesses.", async () => { | ||
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/account_accesses`; | ||
const expectedResponseData = [responseData]; | ||
|
||
expect.assertions(2); | ||
|
||
mock.onGet(endpoint).reply(200, expectedResponseData); | ||
const result = await accountAccessesAPI.listAccountAccesses(); | ||
|
||
expect(mock.history.get[0].url).toEqual(endpoint); | ||
expect(result).toEqual(expectedResponseData); | ||
}); | ||
|
||
it("successfully gets list of user and invite account accesses with filters.", async () => { | ||
const filters = { | ||
domainUuids: ["mock-domainUuids"], | ||
inboxIds: ["mock-inboxIds"], | ||
projectIds: ["mock-projectIds"], | ||
}; | ||
|
||
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/account_accesses`; | ||
|
||
expect.assertions(3); | ||
|
||
mock.onGet(endpoint).reply(200, responseData); | ||
|
||
const result = await accountAccessesAPI.listAccountAccesses(filters); | ||
const expectedParams = { | ||
domain_uuids: filters.domainUuids, | ||
inbox_ids: filters.inboxIds, | ||
project_ids: filters.projectIds, | ||
}; | ||
|
||
expect(mock.history.get[0].url).toEqual(endpoint); | ||
expect(mock.history.get[0].params).toEqual(expectedParams); | ||
expect(result).toEqual(responseData); | ||
}); | ||
|
||
it("fails with error.", async () => { | ||
const expectedErrorMessage = "Request failed with status code 404"; | ||
|
||
expect.assertions(2); | ||
|
||
try { | ||
await accountAccessesAPI.listAccountAccesses(); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(MailtrapError); | ||
|
||
if (error instanceof MailtrapError) { | ||
expect(error.message).toEqual(expectedErrorMessage); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
describe("removeAccountAccess(): ", () => { | ||
it("removes account access.", async () => { | ||
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/account_accesses/${accountAccessId}`; | ||
|
||
expect.assertions(2); | ||
|
||
mock.onDelete(endpoint).reply(200, responseData); | ||
const result = await accountAccessesAPI.removeAccountAccess( | ||
accountAccessId | ||
); | ||
|
||
expect(mock.history.delete[0].url).toEqual(endpoint); | ||
expect(result).toEqual(responseData); | ||
}); | ||
|
||
it("fails with error.", async () => { | ||
const expectedErrorMessage = "Request failed with status code 404"; | ||
|
||
expect.assertions(2); | ||
|
||
try { | ||
await accountAccessesAPI.removeAccountAccess(accountAccessId); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(MailtrapError); | ||
|
||
if (error instanceof MailtrapError) { | ||
expect(error.message).toEqual(expectedErrorMessage); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import axios from "axios"; | ||
import AxiosMockAdapter from "axios-mock-adapter"; | ||
|
||
import Accounts from "../../../../lib/api/resources/Accounts"; | ||
import handleSendingError from "../../../../lib/axios-logger"; | ||
import MailtrapError from "../../../../lib/MailtrapError"; | ||
|
||
import CONFIG from "../../../../config"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
describe("lib/api/resources/Accounts: ", () => { | ||
let mock: AxiosMockAdapter; | ||
|
||
const accountsAPI = new Accounts(axios); | ||
const responseData = { | ||
id: 100, | ||
name: "mock-name", | ||
access_levels: [1], | ||
}; | ||
|
||
describe("class Accounts(): ", () => { | ||
describe("init: ", () => { | ||
it("initalizes with all necessary params.", () => { | ||
expect(accountsAPI).toHaveProperty("getAllAccounts"); | ||
}); | ||
}); | ||
}); | ||
|
||
beforeAll(() => { | ||
/** | ||
* Init Axios interceptors for handling response.data, errors. | ||
*/ | ||
axios.interceptors.response.use( | ||
(response) => response.data, | ||
handleSendingError | ||
); | ||
mock = new AxiosMockAdapter(axios); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
describe("removeAccountAccess(): ", () => { | ||
it("successfully removes account access.", async () => { | ||
const endpoint = `${GENERAL_ENDPOINT}/api/accounts`; | ||
|
||
expect.assertions(2); | ||
|
||
mock.onGet(endpoint).reply(200, responseData); | ||
const result = await accountsAPI.getAllAccounts(); | ||
|
||
expect(mock.history.get[0].url).toEqual(endpoint); | ||
expect(result).toEqual(responseData); | ||
}); | ||
|
||
it("fails with error.", async () => { | ||
const expectedErrorMessage = "Request failed with status code 404"; | ||
|
||
expect.assertions(2); | ||
|
||
try { | ||
await accountsAPI.getAllAccounts(); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(MailtrapError); | ||
|
||
if (error instanceof MailtrapError) { | ||
expect(error.message).toEqual(expectedErrorMessage); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.