Skip to content

Commit

Permalink
Fix up tests for insights support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsharratt committed Mar 18, 2017
1 parent 6e63b1a commit 81134e7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 168 deletions.
3 changes: 0 additions & 3 deletions src/adapters/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fetch from 'node-fetch';


export default class Logger {
constructor(namespace, credentials = {}) {
this.namespace = namespace;
Expand All @@ -16,8 +15,6 @@ export default class Logger {
},
body: JSON.stringify(json),
});
} else {
console.log(JSON.stringify(json)); // eslint-disable-line
}
}

Expand Down
49 changes: 22 additions & 27 deletions test/adapters/logger.test.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
/* eslint-disable no-underscore-dangle */
import AWS from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
import Subject from '../../src/adapters/logger';

describe('Logger', () => {
let user;
let clock;
let awsSpy;
let publishStub;
let fetchStub;

beforeEach(() => {
user = {
name: 'foo',
avatar: 'https://example.com',
};

awsSpy = {
SNS: spy(() => {
publishStub = stub().returns({ promise: () => Promise.resolve() });

const awsSNSInstance = createStubInstance(AWS.S3);
awsSNSInstance.publish = publishStub;

return awsSNSInstance;
}),
};
fetchStub = stub();

clock = useFakeTimers();

Subject.__Rewire__('AWS', awsSpy);
Subject.__Rewire__('fetch', fetchStub);
});

describe('#info()', () => {
it('should call AWS with correct parameters', async () => {
it('should call insights logging endpoint with correct parameters', async () => {
const subject = new Subject('foo:bar', {
region: 'foo-region',
topic: 'bar-topic',
clientId: 'foo-client-id',
secret: 'bar-secret',
});

await subject.info(user, { foo: 'bar' });

assert(publishStub.calledWithExactly({
Message: '{"user":{"name":"foo","avatar":"https://example.com"},"timestamp":"1970-01-01T00:00:00.000Z","level":"info","namespace":"info:foo:bar","body":{"foo":"bar"}}',
TopicArn: 'bar-topic',
assert(fetchStub.calledWithExactly('https://log.codebox.sh/v1/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{"user":{"name":"foo","avatar":"https://example.com"},"credentials":{"clientId":"foo-client-id","secret":"bar-secret"},"timestamp":"1970-01-01T00:00:00.000Z","level":"info","namespace":"info:foo:bar","body":{"foo":"bar"}}',
}));
});
});

describe('#error()', () => {
it('should call AWS with correct parameters', async () => {
it('should call insights logging endpoint with correct parameters', async () => {
const subject = new Subject('foo:bar', {
region: 'foo-region',
topic: 'bar-topic',
clientId: 'foo-client-id',
secret: 'bar-secret',
});

const expectedError = new Error('Foo Bar');
expectedError.stack = 'foo bar stack';

await subject.error(user, expectedError);

assert(publishStub.calledWithExactly({
Message: '{"user":{"name":"foo","avatar":"https://example.com"},"timestamp":"1970-01-01T00:00:00.000Z","level":"error","namespace":"error:foo:bar","body":{"message":"Foo Bar","stack":"foo bar stack"}}',
TopicArn: 'bar-topic',
assert(fetchStub.calledWithExactly('https://log.codebox.sh/v1/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{"user":{"name":"foo","avatar":"https://example.com"},"credentials":{"clientId":"foo-client-id","secret":"bar-secret"},"timestamp":"1970-01-01T00:00:00.000Z","level":"error","namespace":"error:foo:bar","body":{"message":"Foo Bar","stack":"foo bar stack"}}',
}));
});
});

afterEach(() => {
clock.restore();
Subject.__ResetDependency__('AWS');
Subject.__ResetDependency__('fetch');
});
});
2 changes: 2 additions & 0 deletions test/get/lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('GET /registry/{name}', () => {
user: stub(),
log: {
error: stub(),
info: stub(),
},
npm: npmStub,
storage: storageStub,
Expand Down Expand Up @@ -124,6 +125,7 @@ describe('GET /registry/{name}', () => {
user: stub(),
log: {
error: stub(),
info: stub(),
},
npm: stub(),
storage: storageStub,
Expand Down
138 changes: 0 additions & 138 deletions test/serverless_plugins/log-topic/index.test.js

This file was deleted.

0 comments on commit 81134e7

Please sign in to comment.