Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Mar 24, 2017
1 parent 95c3105 commit 50debe7
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 10 deletions.
27 changes: 27 additions & 0 deletions test/apis/memberships.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

test('memberships.ROLES', t => {
const rokka = rka()
t.deepEqual(rokka.memberships.ROLES, { READ: 'read', WRITE: 'write', ADMIN: 'admin' })
})

test('memberships.create', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.memberships.create('myorg', 'user@example.org', rokka.memberships.ROLES.ADMIN)

const expectedArgs = {
method: 'PUT',
uri: 'https://api.rokka.io/organizations/myorg/memberships/user@example.org',
body: { role: 'admin' },
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
34 changes: 34 additions & 0 deletions test/apis/operations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

const knownOperations = ['resize', 'rotate', 'dropshadow', 'trim', 'crop', 'noop']

test('known operation functions exist', t => {
t.plan(knownOperations.length)

const rokka = rka({ apiKey: 'APIKEY' })

knownOperations.forEach(key => {
t.true(typeof rokka.operations[key] === 'function')
})
})

test('operations.list', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.operations.list('myorg')

const expectedArgs = {
method: 'GET',
uri: 'https://api.rokka.io/operations',
body: null,
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
22 changes: 22 additions & 0 deletions test/apis/organizations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

test('organizations.get', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.organizations.get('myorg')

const expectedArgs = {
method: 'GET',
uri: 'https://api.rokka.io/organizations/myorg',
body: null,
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
24 changes: 24 additions & 0 deletions test/apis/render.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava'

import rka from '../../src'

test('render.getUrl using stack', t => {
const rokka = rka({ apiKey: 'APIKEY' })

const url = rokka.render.getUrl('myorg', 'c421f4e8cefe0fd3aab22832f51e85bacda0a47a', 'png', 'mystack')

t.is(url, 'https://myorg.rokka.io/mystack/c421f4e8cefe0fd3aab22832f51e85bacda0a47a.png')
})

test('render.getUrl using custom operations', t => {
const rokka = rka({ apiKey: 'APIKEY' })

const operations = [
rokka.operations.rotate(45),
rokka.operations.resize(100, 100)
]

const url = rokka.render.getUrl('myorg', 'c421f4e8cefe0fd3aab22832f51e85bacda0a47a', 'png', operations)

t.not(url.indexOf('https://myorg.rokka.io/dynamic/rotate-angle-45--resize-width-100-height-100/c42'), -1)
})
72 changes: 72 additions & 0 deletions test/apis/stacks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

test('stacks.list', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.stacks.list('myorg')

const expectedArgs = {
method: 'GET',
uri: 'https://api.rokka.io/stacks/myorg',
body: null,
qs: {}
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('stacks.get', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.stacks.get('myorg', 'mystack')

const expectedArgs = {
method: 'GET',
uri: 'https://api.rokka.io/stacks/myorg/mystack',
body: null,
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('stacks.create', t => {
const rokka = rka({ apiKey: 'APIKEY' })

const operations = [
rokka.operations.rotate(45),
rokka.operations.resize(100, 100)
]

rokka.stacks.create('myorg', 'mystack', operations)

const expectedArgs = {
method: 'PUT',
uri: 'https://api.rokka.io/stacks/myorg/mystack',
body: operations,
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('stacks.delete', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.stacks.delete('myorg', 'mystack')

const expectedArgs = {
method: 'DELETE',
uri: 'https://api.rokka.io/stacks/myorg/mystack',
body: null,
qs: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
22 changes: 22 additions & 0 deletions test/apis/stats.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

test('stats.get', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.stats.get('myorg', '2017-01-01', '2017-01-31')

const expectedArgs = {
method: 'GET',
uri: 'https://api.rokka.io/stats/myorg',
qs: {from: '2017-01-01', to: '2017-01-31'},
body: null
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
21 changes: 21 additions & 0 deletions test/apis/users.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava'
import td from 'testdouble'

import * as transport from '../../src/transport'
const requestStub = td.replace(transport, 'default')

import rka from '../../src'

test('users.create', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.users.create('user@example.org')

const expectedArgs = {
method: 'POST',
uri: 'https://api.rokka.io/users',
body: { email: 'user@example.org' }
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})
22 changes: 12 additions & 10 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import test from 'ava'
import td from 'testdouble'

import * as apis from '../src/apis'
const modules = td.replace(apis, 'default')
import _rokka from '../src'
const apisStub = td.replace(apis, 'default')

import rokka from '../src'

test('default options', t => {
const expectedState = {
Expand All @@ -13,21 +14,22 @@ test('default options', t => {
apiVersion: 1
}

_rokka()
rokka()

td.verify(modules(td.matchers.contains(expectedState)), { times: 1 })
td.verify(apisStub(td.matchers.contains(expectedState)), { times: 1 })
})

test('custom options', t => {
const customOptions = {
apiKey: 'APIKEY',
apiHost: 'https://api.example.org',
renderHost: 'https://{organization}.example.org',
apiVersion: 2
}

_rokka(customOptions)
rokka(customOptions)

td.verify(modules(td.matchers.contains(customOptions)))
td.verify(apisStub(td.matchers.contains(customOptions)))
})

test('default transport options', t => {
Expand All @@ -40,9 +42,9 @@ test('default transport options', t => {
}
}

_rokka()
rokka()

td.verify(modules(td.matchers.contains(expectedState)))
td.verify(apisStub(td.matchers.contains(expectedState)))
})

test('custom transport options', t => {
Expand All @@ -53,7 +55,7 @@ test('custom transport options', t => {
randomize: false
}

_rokka({ transport: transportOptions })
rokka({ transport: transportOptions })

td.verify(modules(td.matchers.contains({ transportOptions })))
td.verify(apisStub(td.matchers.contains({ transportOptions })))
})
79 changes: 79 additions & 0 deletions test/request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import test from 'ava'
import td from 'testdouble'

import * as apis from '../src/apis'
const apisStub = td.replace(apis, 'default')

import * as transport from '../src/transport'
const requestStub = td.replace(transport, 'default')

import rokka from '../src'

test('simple authorized request invocation', t => {
rokka({ apiKey: 'APIKEY' })
const request = td.explain(apisStub).calls.slice(-1)[0].args[0].request

request('get', 'test')
})

test('simple non-authorized request invocation', t => {
rokka()

const request = td.explain(apisStub).calls.slice(-1)[0].args[0].request

request('get', 'test', null, null, { noAuthHeaders: true })
})

test('missing API key', t => {
rokka()

const request = td.explain(apisStub).calls.slice(-1)[0].args[0].request

t.throws(request('get', 'test'), 'Missing required property `apiKey`')
})

test('request argument handling', t => {
const apiHost = 'https://api.example.org'
const apiVersion = 23
const apiKey = 'APIKEY'
const path = 'test'
const method = 'post'
const body = { test: 'value' }
const queryParams = { limit: 100, offset: 200 }

const expectedArgs = {
method,
uri: `${apiHost}/${path}`,
body,
qs: queryParams,
headers: { 'Api-Version': apiVersion, 'Api-Key': apiKey },
json: true,
resolveWithFullResponse: true,
timeout: 30000
}

rokka({ apiHost, apiKey, apiVersion })

const request = td.explain(apisStub).calls.slice(-1)[0].args[0].request

request(method, path, body, queryParams)

td.verify(requestStub(expectedArgs, td.matchers.isA(Object)))
})

test('retry options handling', t => {
const transportOptions = {
retries: 23,
minTimeout: 2323,
maxTimeout: 232323,
randomize: false
}

rokka({ apiKey: 'APIKEY', transport: transportOptions })

const request = td.explain(apisStub).calls.slice(-1)[0].args[0].request

request('get', 'test')

td.verify(requestStub(td.matchers.isA(Object), td.matchers.contains(transportOptions)))
})

0 comments on commit 50debe7

Please sign in to comment.