-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Option network method createApiToken (#88)
- Loading branch information
Showing
11 changed files
with
587 additions
and
349 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,77 @@ | ||
import { describe } from 'riteway' | ||
import { getOptions } from './Frost' | ||
import { Method } from './utils/utils' | ||
|
||
describe('Frost getOptions()', async (should: any) => { | ||
const { assert } = should('') | ||
|
||
for (const method of Object.keys(Method)) { | ||
const currentMethod = (Method as any)[method] | ||
{ | ||
const actual = getOptions(currentMethod) | ||
const expected = { | ||
method: currentMethod, | ||
headers: { _headers: { 'content-type': ['application/json'] } }, | ||
} | ||
|
||
assert({ | ||
given: `getOptions with the method ${currentMethod}`, | ||
should: `return with the method: '${currentMethod}' and the header with 'Content-Type': 'application/json'`, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
} | ||
|
||
{ | ||
const actual = getOptions(Method.POST, {}).headers | ||
const expected = { _headers: { 'content-type': ['application/json'] } } | ||
|
||
assert({ | ||
given: 'getOptions with an object empty header', | ||
should: `return the header with 'Content-Type': 'application/json'`, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const token = { token: 'token' } | ||
|
||
const actual = getOptions(Method.POST, token).headers | ||
const expected = { _headers: { 'content-type': ['application/json'], token: ['token'] } } | ||
|
||
assert({ | ||
given: 'getOptions with an object empty header', | ||
should: `return the header with 'Content-Type': 'application/json' and the object token`, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const actual = getOptions(Method.POST, {}, {}).body | ||
const expected: any = undefined | ||
|
||
assert({ | ||
given: 'getOptions with an object empty body', | ||
should: 'be body undefined', | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const network = { network: 'mainnet' } | ||
|
||
const actual = getOptions(Method.POST, {}, network).body | ||
const expected = '{"network":"mainnet"}' | ||
|
||
assert({ | ||
given: 'getOptions with object network in the body', | ||
should: 'return body with the objet network', | ||
actual, | ||
expected, | ||
}) | ||
} | ||
}) |
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
This file was deleted.
Oops, something went wrong.
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
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
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,2 @@ | ||
import './unit/index' | ||
import './integration/index' |
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,116 @@ | ||
import * as nock from 'nock' | ||
import { describe } from 'riteway' | ||
import { Frost } from '../../src/Frost' | ||
import { Path, Network } from '../../src/utils/utils' | ||
|
||
describe('Frost createApiToken()', async (should: any) => { | ||
const { assert } = should('') | ||
const host = 'https://api.frost.po.et' | ||
|
||
const config = { | ||
email: '[email protected]', | ||
password: 'test', | ||
timeout: 0, | ||
host, | ||
} | ||
|
||
{ | ||
const serverResponse = { apiToken: '1' } | ||
const token = 'test' | ||
|
||
// TODO: we have to do it with the real network, no mock | ||
nock(host) | ||
.post(Path.TOKENS) | ||
.reply(200, serverResponse) | ||
|
||
const frost = new Frost(config) | ||
|
||
const actual = await frost.createApiToken(token) | ||
const expected = serverResponse | ||
|
||
assert({ | ||
given: 'a status code of 200', | ||
should: 'return the object with apiToken equal 1', | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const serverResponse = { apiToken: '1' } | ||
const token = 'test' | ||
let requestBody = {} | ||
|
||
nock(host) | ||
.post(Path.TOKENS, (body: {}) => { | ||
requestBody = body | ||
return body | ||
}) | ||
.reply(200, serverResponse) | ||
|
||
const frost = new Frost(config) | ||
await frost.createApiToken(token) | ||
|
||
const actual = requestBody | ||
const expected = {} | ||
|
||
assert({ | ||
given: 'a request without network property', | ||
should: 'be the request body empty', | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const serverResponse = { apiToken: '1' } | ||
const token = 'test' | ||
let requestBody = {} | ||
|
||
nock(host) | ||
.post(Path.TOKENS, (body: {}) => { | ||
requestBody = body | ||
return body | ||
}) | ||
.reply(200, serverResponse) | ||
|
||
const frost = new Frost(config) | ||
await frost.createApiToken(token, Network.MAINNET) | ||
|
||
const actual = requestBody | ||
const expected = { network: Network.MAINNET } | ||
|
||
assert({ | ||
given: `a request with the ${Network.MAINNET} network property`, | ||
should: `have the request body an object with { network: '${Network.MAINNET}' }`, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
|
||
{ | ||
const token = 'test' | ||
|
||
nock(host) | ||
.post(Path.TOKENS) | ||
.reply(500) | ||
|
||
const frost = new Frost(config) | ||
|
||
let actual = '' | ||
const expected = 'string' | ||
|
||
try { | ||
await frost.createApiToken(token) | ||
} catch(e) { | ||
actual = typeof e | ||
} | ||
|
||
assert({ | ||
given: 'a status code of 500', | ||
should: `return a string`, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
}) |
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 @@ | ||
import './createApiToken.test' |
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
// All unit test files should be imported here. For example, | ||
// import '../../src/foo.test' | ||
import '../../src/canary.test' | ||
import '../../src/utils/utils.test' | ||
import '../../src/Frost.test' |