Skip to content

Commit

Permalink
Move old auth stuff to new module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Wood committed Apr 17, 2017
1 parent ee9d138 commit 2190ef1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 84 deletions.
36 changes: 36 additions & 0 deletions lib/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@
*/
module.exports = api => {
return {
/**
* Generate a URL for client side authentication.
* @param {string} - Client ID token
* @param {string} redirectURI - URI you want to be redirected at
* @param {Object} [scope] - The scope you want to request
* @returns {string} - A ready-to-use authentication url
*/
authenticateClientURL(
clientId = "",
redirectURI = "",
scope = {
basic: true,
stream: true,
write_post: true,
follow: true,
update_profile: true,
presence: true,
messages: true,
public_messages: true
}
) {
if (clientId.length < 1) {
throw new Error("You need a client ID for this request");
} else if (redirectURI.length < 1) {
throw new Error("You need a redirect URI for this request");
}

let url = `https://pnut.io/oauth/authenticate?`;
url += `client_id=${clientId}`;
url += `&redirect_uri=${redirectURI}`;
url += `&scope=${Object.keys(scope).join(",")}`;
url += `&response_type=token`;

return url;
},

/**
* Request an app access token
* @param {string} clientId - The client id
Expand Down
36 changes: 0 additions & 36 deletions lib/pnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,42 +107,6 @@ const pnut = () => {
httpMethod: method,
data: data
});
},

/**
* Generate a URL for client side authentication.
* @param {string} - Client ID token
* @param {string} redirectURI - URI you want to be redirected at
* @param {Object} [scope] - The scope you want to request
* @returns {string} - A ready-to-use authentication url
*/
authenticateClientURL(
clientId = "",
redirectURI = "",
scope = {
basic: true,
stream: true,
write_post: true,
follow: true,
update_profile: true,
presence: true,
messages: true,
public_messages: true
}
) {
if (clientId.length < 1) {
throw new Error("You need a client ID for this request");
} else if (redirectURI.length < 1) {
throw new Error("You need a redirect URI for this request");
}

let url = `https://pnut.io/oauth/authenticate?`;
url += `client_id=${clientId}`;
url += `&redirect_uri=${redirectURI}`;
url += `&scope=${Object.keys(scope).join(",")}`;
url += `&response_type=token`;

return url;
}
},
authentication(api),
Expand Down
20 changes: 20 additions & 0 deletions test/authentication_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const chaiAsPromised = require("chai-as-promised");
const nock = require("nock");
chai.use(chaiAsPromised);
chai.should();
const expect = chai.expect;

const pnut = require("../lib/pnut");

Expand All @@ -21,6 +22,25 @@ after(function() {
});

describe("Authentication", () => {
it("should fail when no client id is given", () => {
expect(() => pnut.authenticateClientURL("", "http://github.com")).to.throw(
Error
);
});

it("should fail if no redirect uri is given", () => {
expect(() => pnut.authenticateClientURL("sometoken", "")).to.throw(Error);
});

it("should give back a correct url where the client can authenticate", () => {
let expectedURL =
"https://pnut.io/oauth/authenticate?client_id=mytoken&redirect_uri=http://github.com&scope=basic,stream,write_post,follow,update_profile,presence,messages,public_messages&response_type=token";

expect(pnut.authenticateClientURL("mytoken", "http://github.com")).to.equal(
expectedURL
);
});

it("should be able to request an app access token", () => {
return pnut.requestAppAccessToken("foo", "bar").should.become({});
});
Expand Down
71 changes: 23 additions & 48 deletions test/pnut_test.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,47 @@
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const nock = require('nock');
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const nock = require("nock");
chai.use(chaiAsPromised);
chai.should();
const expect = chai.expect;

const pnut = require('../lib/pnut');
const pnut = require("../lib/pnut");

before(function () {
let base = 'https://api.pnut.io/v0';
before(function() {
let base = "https://api.pnut.io/v0";

nock(base)
.get('/somewhere')
.reply(200, {});
nock(base).get("/somewhere").reply(200, {});

nock(base)
.post('/somewhere')
.reply(200, {});
nock(base).post("/somewhere").reply(200, {});

nock(base)
.put('/somewhere')
.reply(200, {});
nock(base).put("/somewhere").reply(200, {});

nock(base)
.patch('/somewhere')
.reply(200, {});
nock(base).patch("/somewhere").reply(200, {});
});

after(function () {
after(function() {
nock.cleanAll();
});

describe('The pnut-butter library', () => {
it('should be able to send a custom GET request', () => {
return pnut.custom('/somewhere').should.become({});
describe("The pnut-butter library", () => {
it("should be able to send a custom GET request", () => {
return pnut.custom("/somewhere").should.become({});
});

it('should be able to send a custom POST request', () => {
return pnut.custom('/somewhere', 'POST', {
text: 'sometext'
it("should be able to send a custom POST request", () => {
return pnut.custom("/somewhere", "POST", {
text: "sometext"
});
});

it('should be able to send a custom PUT request', () => {
return pnut.custom('/somewhere', 'PUT', {
text: 'sometext'
it("should be able to send a custom PUT request", () => {
return pnut.custom("/somewhere", "PUT", {
text: "sometext"
});
});

it('should be able to send a custom PATCH request', () => {
return pnut.custom('/somewhere', 'PATCH', {
text: 'sometext'
it("should be able to send a custom PATCH request", () => {
return pnut.custom("/somewhere", "PATCH", {
text: "sometext"
});
});
});

describe('Authentication', () => {
it('should fail when no client id is given', () => {
expect(() => pnut.authenticateClientURL('', 'http://github.com')).to.throw(Error);
});

it('should fail if no redirect uri is given', () => {
expect(() => pnut.authenticateClientURL('sometoken', '')).to.throw(Error);
});

it('should give back a correct url where the client can authenticate', () => {
let expectedURL = 'https://pnut.io/oauth/authenticate?client_id=mytoken&redirect_uri=http://github.com&scope=basic,stream,write_post,follow,update_profile,presence,messages,public_messages&response_type=token';

expect(pnut.authenticateClientURL('mytoken', 'http://github.com')).to.equal(expectedURL);
});
});

0 comments on commit 2190ef1

Please sign in to comment.