Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): fix failing unit test for request query #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{
"name": "medium-sdk"
, "description": "NodeJS client for the Medium app"
, "version": "0.0.4"
, "homepage": "https://github.com/medium/medium-sdk-nodejs"
, "author": "Jamie Talbot <[email protected]> (https://github.com/majelbstoat)"
, "keywords": ["medium", "api", "writing"]
, "main": "index.js"
, "repository": {
"type": "git"
, "url": "https://github.com/medium/medium-sdk-nodejs.git"
}
, "devDependencies": {
"mocha": "^2.2.5"
, "should": "^7.1"
, "nock": "^2.17"
"name": "medium-sdk",
"description": "NodeJS client for the Medium app",
"version": "0.0.4",
"homepage": "https://github.com/medium/medium-sdk-nodejs",
"author": "Jamie Talbot <[email protected]> (https://github.com/majelbstoat)",
"keywords": [
"medium",
"api",
"writing"
],
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medium/medium-sdk-nodejs.git"
},
"devDependencies": {
"mocha": "^2.2.5",
"nock": "^2.17",
"should": "^13.2.3"
},
"scripts": {
"test": "mocha"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a npm command so anyone can run npm run test

}
}
78 changes: 39 additions & 39 deletions test/mediumClient_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ describe('MediumClient - constructor', function () {
})

it('should throw a MediumError when only clientId is provided', function (done) {
(function () { new medium.MediumClient({clientId: 'xxx'}) }).should.throw(medium.MediumError)
(function () { new medium.MediumClient({ clientId: 'xxx' }) }).should.throw(medium.MediumError)
done()
})

it('should throw a MediumError when only clientSecret is provided', function (done) {
(function () { new medium.MediumClient({clientSecret: 'yyy'}) }).should.throw(medium.MediumError)
(function () { new medium.MediumClient({ clientSecret: 'yyy' }) }).should.throw(medium.MediumError)
done()
})

it('should succeed when both clientId and clientSecret are provided', function (done) {
var client = new medium.MediumClient({clientId: 'xxx', clientSecret: 'yyy'})
var client = new medium.MediumClient({ clientId: 'xxx', clientSecret: 'yyy' })
done()
})
})
Expand All @@ -41,7 +41,7 @@ describe('MediumClient - methods', function () {
var client

beforeEach(function () {
client = new medium.MediumClient({clientId: clientId, clientSecret: clientSecret})
client = new medium.MediumClient({ clientId: clientId, clientSecret: clientSecret })
nock.disableNetConnect()
})

Expand All @@ -52,7 +52,7 @@ describe('MediumClient - methods', function () {

describe('#setAccessToken', function () {

it ('sets the access token', function (done) {
it('sets the access token', function (done) {
var token = "new token"
client.setAccessToken(token)
client._accessToken.should.be.String().and.equal(token)
Expand All @@ -62,7 +62,7 @@ describe('MediumClient - methods', function () {

describe('#getAuthorizationUrl', function () {

it ('returns a valid URL for fetching', function (done) {
it('returns a valid URL for fetching', function (done) {
var state = "state"
var redirectUrl = "https://example.com/callback"
var scope = [medium.Scope.BASIC_PROFILE, medium.Scope.LIST_PUBLICATIONS, medium.Scope.PUBLISH_POST]
Expand All @@ -71,20 +71,20 @@ describe('MediumClient - methods', function () {
authUrl.protocol.should.equal('https:')
authUrl.hostname.should.equal('medium.com')
authUrl.pathname.should.equal('/m/oauth/authorize')
authUrl.query.should.deepEqual({
should.equal(JSON.stringify(authUrl.query), JSON.stringify({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I changed, others are indentation fixes, deepEqual not working with objects, don't know, are they checking the reference as well. So I had to do JSON.stringify!

client_id: clientId,
scope: scope.join(','),
response_type: 'code',
state: state,
redirect_uri: redirectUrl
})
}));
done()
})
})

describe('#exchangeAuthorizationCode', function () {

it ('makes a request for authorization_code and sets the access token from response', function (done) {
it('makes a request for authorization_code and sets the access token from response', function (done) {
var code = '12345'
var grantType = 'authorization_code'
var redirectUrl = 'https://example.com/callback'
Expand All @@ -105,8 +105,8 @@ describe('MediumClient - methods', function () {
refresh_token: refreshToken
}
var request = nock('https://api.medium.com/', {
'Content-Type': 'application/x-www-form-urlencoded'
})
'Content-Type': 'application/x-www-form-urlencoded'
})
.post('/v1/tokens', requestBody)
.reply(201, responseBody)

Expand All @@ -122,7 +122,7 @@ describe('MediumClient - methods', function () {

describe('#exchangeRefreshToken', function () {

it ('makes a request for authorization_code and sets the access token from response', function (done) {
it('makes a request for authorization_code and sets the access token from response', function (done) {
var refreshToken = 'fedcba'
var accessToken = 'lkjihg'

Expand All @@ -139,8 +139,8 @@ describe('MediumClient - methods', function () {
refresh_token: refreshToken
}
var request = nock('https://api.medium.com/', {
'Content-Type': 'application/x-www-form-urlencoded'
})
'Content-Type': 'application/x-www-form-urlencoded'
})
.post('/v1/tokens', requestBody)
.reply(201, responseBody)

Expand All @@ -155,7 +155,7 @@ describe('MediumClient - methods', function () {
})

describe('#getUser', function () {
it ('gets the information from expected URL and returns contents of data envelope', function (done) {
it('gets the information from expected URL and returns contents of data envelope', function (done) {
var response = { data: 'response data' }

var request = nock('https://api.medium.com')
Expand All @@ -173,20 +173,20 @@ describe('MediumClient - methods', function () {

describe('#getPublicationsForUser', function () {

it ('throws a MediumError when no user ID is provided', function (done) {
it('throws a MediumError when no user ID is provided', function (done) {
(function () { client.getPublicationsForUser({}) }).should.throw(medium.MediumError)
done()
})

it ('makes a proper GET request to the Medium API and returns contents of data envelope when valid options are provided', function (done) {
it('makes a proper GET request to the Medium API and returns contents of data envelope when valid options are provided', function (done) {
var userId = '123456'
var response = { data: 'response data' }

var request = nock('https://api.medium.com/')
.get('/v1/users/' + userId + '/publications')
.reply(200, response)

client.getPublicationsForUser({userId: userId}, function (err, data) {
client.getPublicationsForUser({ userId: userId }, function (err, data) {
if (err) throw err
data.should.deepEqual(response['data'])
done()
Expand All @@ -197,12 +197,12 @@ describe('MediumClient - methods', function () {

describe('#getContributorsForPublication', function () {

it ('throws a MediumError when no publication ID is provided', function (done) {
it('throws a MediumError when no publication ID is provided', function (done) {
(function () { client.getContributorsForPublication({}) }).should.throw(medium.MediumError)
done()
})

it ('makes a proper GET request to the Medium API and returns contents of data envelope', function (done) {
it('makes a proper GET request to the Medium API and returns contents of data envelope', function (done) {
var options = { publicationId: 'abcdef' }
var response = { data: 'response data' }
var request = nock('https://api.medium.com/')
Expand All @@ -220,7 +220,7 @@ describe('MediumClient - methods', function () {

describe('#createPost', function () {

it ('makes a proper POST request to the Medium API and returns contents of data envelope', function (done) {
it('makes a proper POST request to the Medium API and returns contents of data envelope', function (done) {
var options = {
userId: '123456',
title: 'new post title',
Expand All @@ -235,14 +235,14 @@ describe('MediumClient - methods', function () {
var response = { data: 'response data' }
var request = nock('https://api.medium.com/')
.post('/v1/users/' + options.userId + '/posts', {
title: options.title,
content: options.content,
contentFormat: options.contentFormat,
tags: options.tags,
canonicalUrl: options.canonicalUrl,
publishedAt: options.publishedAt,
publishStatus: options.publishStatus,
license: options.license
title: options.title,
content: options.content,
contentFormat: options.contentFormat,
tags: options.tags,
canonicalUrl: options.canonicalUrl,
publishedAt: options.publishedAt,
publishStatus: options.publishStatus,
license: options.license
})
.reply(200, response)

Expand All @@ -257,12 +257,12 @@ describe('MediumClient - methods', function () {

describe('#createPostInPublication', function () {

it ('should throw an error when no publication ID is provided', function (done) {
it('should throw an error when no publication ID is provided', function (done) {
(function () { client.createPostInPublication({}) }).should.throw(medium.MediumError)
done()
})

it ('makes a proper POST request to the Medium API and returns contents of data envelope', function (done) {
it('makes a proper POST request to the Medium API and returns contents of data envelope', function (done) {
var options = {
publicationId: 'abcdef',
title: 'new post title',
Expand All @@ -277,14 +277,14 @@ describe('MediumClient - methods', function () {
var response = { data: 'response data' }
var request = nock('https://api.medium.com/')
.post('/v1/publications/' + options.publicationId + '/posts', {
title: options.title,
content: options.content,
contentFormat: options.contentFormat,
tags: options.tags,
canonicalUrl: options.canonicalUrl,
publishedAt: options.publishedAt,
publishStatus: options.publishStatus,
license: options.license
title: options.title,
content: options.content,
contentFormat: options.contentFormat,
tags: options.tags,
canonicalUrl: options.canonicalUrl,
publishedAt: options.publishedAt,
publishStatus: options.publishStatus,
license: options.license
})
.reply(200, response)

Expand Down