-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: separate request creation from execution for easier testing
- Loading branch information
1 parent
ab1788b
commit 5672642
Showing
2 changed files
with
103 additions
and
81 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 |
---|---|---|
@@ -1,91 +1,112 @@ | ||
import { expect, test } from '@oclif/test' | ||
import { requestAPI } from './request' | ||
import { createRequestConfig, requestAPI } from './request' | ||
import { HttpStatusCode } from 'axios' | ||
import * as requestUtils from './requestUtils' | ||
import Auth from './auth' | ||
import { Profile } from '../types' | ||
|
||
describe('requestAPI', () => { | ||
describe('createRequestConfig', () => { | ||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456', | ||
ZENDESK_OAUTH_TOKEN: 'good_token' | ||
}) | ||
.stub(Auth, 'getLoggedInProfile', () => ({ subdomain: 'z3ntest2', domain: 'example.com' })) | ||
.nock('https://z3ntest.zendesk.com', api => { | ||
api | ||
.get('/api/v2/me') | ||
.reply(function () { | ||
expect(this.req.headers.authorization).to.equal('Bearer good_token') | ||
return [200] | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.it('should create a request with an OAuth token', async () => { | ||
const req = await createRequestConfig('api/v2/me') | ||
expect(req.headers.Authorization).to.equal('Bearer good_token') | ||
}) | ||
.do(async () => { | ||
await requestAPI('api/v2/me', { method: 'GET' }) | ||
|
||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_OAUTH_TOKEN: 'good_token' | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.it('should be able to attach extra headers to request', async () => { | ||
const req = await createRequestConfig('api/v2/me', { | ||
headers: { foo: 'bar' }, | ||
method: 'GET' | ||
}) | ||
expect(req.headers.Authorization).to.equal('Bearer good_token') | ||
expect(req.headers.foo).to.equal('bar') | ||
}) | ||
.it('should make a request with Auth token') | ||
|
||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_DOMAIN: 'expected.com', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(Auth, 'getLoggedInProfile', () => ({ subdomain: 'z3ntest2', domain: 'example.com' })) | ||
.nock('https://z3ntest.zendesk.com', api => { | ||
api | ||
.get('/api/v2/me') | ||
.reply(function () { | ||
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') | ||
expect(this.req.headers.foo).to.equal('bar') | ||
return [200] | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.it('should be able to create a request with a Basic auth token', async () => { | ||
const req = await createRequestConfig('api/v2/me', {}) | ||
expect(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') | ||
}) | ||
.do(async () => { | ||
await requestAPI('api/v2/me', { | ||
headers: { foo: 'bar' }, | ||
method: 'GET' | ||
}) | ||
|
||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_DOMAIN: 'expected.com', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.it('should create a request with the correct domain', async () => { | ||
const req = await createRequestConfig('api/v2/me') | ||
expect(req.baseURL).to.equal('https://z3ntest.expected.com') | ||
}) | ||
.it('should be able to attach extra headers to request') | ||
|
||
test.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_DOMAIN: 'example.com', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(Auth, 'getLoggedInProfile', () => ({ subdomain: 'z3ntest2', domain: 'example.com' })) | ||
.nock('https://z3ntest.example.com', api => { | ||
api | ||
.get('/api/v2/me') | ||
.reply(function () { | ||
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') | ||
return [200] | ||
}) | ||
}).do(async () => { | ||
await requestAPI('api/v2/me', { | ||
method: 'GET' | ||
}) | ||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.it('should use the default domain if ZENDESK_SUBDOMAIN is provided and ZENDESK_DOMAIN is not provided, not the profile domain', async () => { | ||
const req = await createRequestConfig('api/v2/me') | ||
expect(req.baseURL).to.equal('https://z3ntest.zendesk.com') | ||
}) | ||
|
||
test | ||
.env({ | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'ping') | ||
.stub(requestUtils, 'getDomain', () => 'me.com') | ||
.stub(Auth, 'getLoggedInProfile', () : Profile => ({ subdomain: 'ping', domain: 'me.com' })) | ||
.it('should be able to create auth using profile subdomain and domain', async () => { | ||
const req = await createRequestConfig('api/v2/me', {}) | ||
expect(req.baseURL).to.equal('https://ping.me.com') | ||
expect(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') | ||
}) | ||
.it('should make a request to the correct domain') | ||
}) | ||
|
||
test.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456' | ||
}) | ||
.stub(Auth, 'getLoggedInProfile', () => ({ subdomain: 'z3ntest2', domain: 'example.com' })) | ||
describe('requestAPI', () => { | ||
test | ||
.env({ | ||
ZENDESK_SUBDOMAIN: 'z3ntest', | ||
ZENDESK_EMAIL: 'test@zendesk.com', | ||
ZENDESK_API_TOKEN: '123456', | ||
ZENDESK_OAUTH_TOKEN: 'good_token' | ||
}) | ||
.stub(requestUtils, 'getSubdomain', () => 'fake') | ||
.stub(requestUtils, 'getDomain', () => 'fake.com') | ||
.nock('https://z3ntest.zendesk.com', api => { | ||
api | ||
.get('/api/v2/me') | ||
.reply(function () { | ||
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') | ||
return [200] | ||
}) | ||
}).do(async () => { | ||
await requestAPI('api/v2/me', { | ||
method: 'GET' | ||
}) | ||
.reply(HttpStatusCode.Ok) | ||
}) | ||
.it('should call an http endpoint', async () => { | ||
const response = await requestAPI('api/v2/me', { method: 'GET' }) | ||
expect(response.status).to.equal(HttpStatusCode.Ok) | ||
}) | ||
.it('should not use the domain stored in current in profile if ZENDESK_SUBDOMAIN is provided and ZENDESK_DOMAIN is not provided') | ||
}) |
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