Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Commit 9dc933b

Browse files
author
Alfonso Garcia
committed
Tests refactor to the adapters & added some tests to the aws generator
1 parent 043d487 commit 9dc933b

File tree

17 files changed

+348
-223
lines changed

17 files changed

+348
-223
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ jspm_packages
4747
# Output of 'npm pack'
4848
*.tgz
4949

50+
# Test Output
51+
serverless.yml
52+
webpack.config.js
53+
5054
###################################
5155

5256
# Visual Studio 2015 cache/options directory

bin/oasp4fn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
let yml_gen = require('../dist/generator/index');
5+
let yml_gen = require('../dist/generators/index');
66
let fs = require('fs');
77
let path = require('path');
88
let _ = require('lodash');
File renamed without changes.

src/generator/index.ts renamed to src/generators/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747
entry: {
4848
`;
4949

50-
export function run(opts: any) {
50+
export function run(opts?: any) {
5151
let options: any = opts ? defineOptions(opts) : _.assign({}, DEFAULTS.aws, { path: DEFAULTS.path });
5252
let dirs = fs.readdirSync(options.path);
5353
dirs = _.reduceRight(<any>dirs, (accumulator: any, value) => {

test/adapters/fn-cognito.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
import { expect } from 'chai';
3+
import fn from '../../src/index';
4+
import cognito from '../../src/adapters/fn-cognito';
5+
import * as AWS from 'aws-sdk';
6+
7+
let region = process.env.REGION || 'us-west-2';
8+
let pool: { clientId: string, userPoolId: string};
9+
let aws_cognito = new AWS.CognitoIdentityServiceProvider({ region: region });
10+
11+
fn.setAuth(cognito);
12+
13+
before(async function () {
14+
this.skip();
15+
let create: Promise<AWS.CognitoIdentityServiceProvider.CreateUserPoolClientResponse | AWS.CognitoIdentityServiceProvider.AdminCreateUserResponse>[] = [];
16+
17+
try {
18+
let res = await aws_cognito.createUserPool({ PoolName: 'oasp4fn'}).promise();
19+
pool = { clientId: '', userPoolId: (<{UserPool: {Id: string}}>res).UserPool.Id};
20+
create.push(aws_cognito.createUserPoolClient({ UserPoolId: pool.userPoolId, ClientName: 'oasp4fn', ExplicitAuthFlows: ['ADMIN_NO_SRP_AUTH']}).promise());
21+
create.push(aws_cognito.adminCreateUser({ UserPoolId: pool.userPoolId, Username: 'user', TemporaryPassword: 'P@ssw0rd'}).promise());
22+
let values = await Promise.all(create);
23+
pool.clientId = <string>(<{UserPoolClient: {ClientId: string}}>values[0]).UserPoolClient.ClientId;
24+
} catch (err) {
25+
return Promise.reject(err);
26+
}
27+
});
28+
29+
describe.skip('login', function () {
30+
this.timeout(0);
31+
32+
it('The function should return a reference to the self object', () => {
33+
expect(fn.login('user', 'P@ssw0rd', pool)).to.be.an('object');
34+
});
35+
36+
it('If the function is succesful, the result should be an object with the tokens provided by cognito', async () => {
37+
const res = await fn.login('user', 'P@ssw0rd', pool)
38+
.promise();
39+
expect(res).to.be.an('object');
40+
expect(res).to.contain.all.keys(['IdToken', 'AccessToken', 'RefreshToken']);
41+
});
42+
43+
it('If the user is incorrect the function should return an error', () => {
44+
return fn.login('us', 'P@ssw0rd', pool)
45+
.then((res: object) => {
46+
expect(res).to.be.undefined;
47+
}, (err: Error) => {
48+
expect(err).to.not.be.null;
49+
});
50+
});
51+
52+
it('If the password is incorrect the function should return an error', () => {
53+
return fn.login('user', 'pass', pool)
54+
.then((res: object) => {
55+
expect(res).to.be.undefined;
56+
}, (err: Error) => {
57+
expect(err).to.not.be.null;
58+
});
59+
});
60+
});
61+
62+
describe.skip('refresh', function () {
63+
this.timeout(0);
64+
let refresh_token: string;
65+
66+
before( async () => {
67+
const res = <{ RefreshToken: string }>await fn.login('user', 'P@ssw0rd', pool)
68+
.promise();
69+
refresh_token = res.RefreshToken;
70+
});
71+
72+
it('The function should return a reference to the self object', () => {
73+
expect(fn.refresh(refresh_token, pool)).to.be.an('object');
74+
});
75+
76+
it('If the function is succesful, the result should be an object with the tokens provided by cognito, except the refresh token', async () => {
77+
const res = await fn.refresh(refresh_token, pool)
78+
.promise();
79+
expect(res).to.be.an('object');
80+
expect(res).to.contain.all.keys(['IdToken', 'AccessToken']);
81+
});
82+
83+
it('If the refresh token is incorrect the function should return an error', () => {
84+
return fn.refresh('some_token', pool)
85+
.then((res: object) => {
86+
expect(res).to.be.undefined;
87+
}, (err: Error) => {
88+
expect(err).to.not.be.null;
89+
});
90+
});
91+
});
92+
93+
after(async () => {
94+
if (pool && pool.userPoolId) {
95+
await aws_cognito.deleteUserPool({ UserPoolId: pool.userPoolId}).promise();
96+
}
97+
});

test/fn-dynamo.ts renamed to test/adapters/fn-dynamo.ts

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import { expect } from 'chai';
3-
import fn from '../src/index';
4-
import dynamo from '../src/adapters/fn-dynamo';
3+
import fn from '../../src/index';
4+
import dynamo from '../../src/adapters/fn-dynamo';
55
let DynamoDB = require('aws-sdk/clients/dynamodb');
66

77
let endpoint = process.env.ENDPOINT || 'http://localhost:4569/';
@@ -133,6 +133,7 @@ describe('table', () => {
133133
expect(res).to.be.an('array');
134134
expect(res).to.have.lengthOf(3);
135135
});
136+
136137
it('If an id passed doesn\'t have an item in the table, an error will be returned', () => {
137138
return fn.table('departments', '7')
138139
.then((res: Department) => {
@@ -200,28 +201,30 @@ describe('orderBy', () => {
200201
});
201202

202203
it("If you pass more than one attribute, the result array should be sorted having in mind all the atributtes", async () => {
203-
const res1 = <Employee[]>await fn.table('employees')
204+
const res = <Employee[]>await fn.table('employees')
204205
.orderBy(['department', 'id'], 'desc')
205206
.promise();
206-
expect(res1).to.be.an('array');
207-
expect(res1).to.have.lengthOf(4);
208-
let i = res1.length;
207+
expect(res).to.be.an('array');
208+
expect(res).to.have.lengthOf(4);
209+
let i = res.length;
209210
while (--i) {
210-
expect(res1[i].department <= res1[i - 1].department).to.be.true;
211-
if (res1[i].department === res1[i - 1].department)
212-
expect(res1[i].id <= res1[i - 1].id).to.be.true;
211+
expect(res[i].department <= res[i - 1].department).to.be.true;
212+
if (res[i].department === res[i - 1].department)
213+
expect(res[i].id <= res[i - 1].id).to.be.true;
213214
}
214-
215-
const res2 = <Employee[]>await fn.table('employees')
215+
});
216+
217+
it("If you pass more than one attribute and more than one order, the result array should be sorted having in mind all the atributtes and all orders", async () => {
218+
const res = <Employee[]>await fn.table('employees')
216219
.orderBy(['department', 'id'], ['desc', 'asc'])
217220
.promise();
218-
expect(res2).to.be.an('array');
219-
expect(res2).to.have.lengthOf(4);
220-
let j = res2.length;
221+
expect(res).to.be.an('array');
222+
expect(res).to.have.lengthOf(4);
223+
let j = res.length;
221224
while (--j) {
222-
expect(res2[j].department <= res2[j - 1].department).to.be.true;
223-
if (res2[j].department === res2[j - 1].department)
224-
expect(res2[j].id >= res2[j - 1].id).to.be.true;
225+
expect(res[j].department <= res[j - 1].department).to.be.true;
226+
if (res[j].department === res[j - 1].department)
227+
expect(res[j].id >= res[j - 1].id).to.be.true;
225228
}
226229
});
227230

@@ -538,15 +541,17 @@ describe('insert', () => {
538541
expect(fn.insert('departments', [{id: '7', name: 'Sales'}, {id: '5', name: 'Comercial'}])).to.be.an('object');
539542
});
540543

541-
it('If the operation is succesful, the result is an id or an array of ids', async () => {
542-
const resArray = await fn.insert('departments', [{ id: '7', name: 'Sales' }, { id: '5', name: 'Comercial' }])
544+
it('If the operation is performed over an array of items, the result is an array of ids', async () => {
545+
const res = await fn.insert('departments', [{ id: '7', name: 'Sales' }, { id: '5', name: 'Comercial' }])
543546
.promise();
544-
expect(resArray).to.be.an('array');
545-
expect(resArray).to.have.lengthOf(2);
546-
547-
const resObject = await fn.insert('departments', { id: '8', name: 'Sales' })
547+
expect(res).to.be.an('array');
548+
expect(res).to.have.lengthOf(2);
549+
});
550+
551+
it('If the operation is performed over a single object, the result is an id', async () => {
552+
const res = await fn.insert('departments', { id: '8', name: 'Sales' })
548553
.promise();
549-
expect(resObject).to.be.a('string').equal('8');
554+
expect(res).to.be.a('string').equal('8');
550555
});
551556

552557
it('If the operation is not starter, the operation insert the items on which we are operating', async () => {
@@ -561,16 +566,18 @@ describe('insert', () => {
561566
expect(res).to.have.lengthOf(7);
562567
});
563568

564-
it('If the operation it\'s done over an empty array, the result should be an empty array', async () => {
565-
const res1 = await fn.insert('employees', [])
569+
it('If an empty array is passed to the operation, the result should be an empty array', async () => {
570+
const res = await fn.insert('employees', [])
566571
.promise();
567-
expect(res1).to.be.an('array').empty;
572+
expect(res).to.be.an('array').empty;
573+
});
568574

569-
const res2 = await fn.table('employees')
575+
it('If the operation is done over an empty array, the result should be an empty array', async () => {
576+
const res = await fn.table('employees')
570577
.where('id', 'x', '=')
571578
.insert()
572579
.promise();
573-
expect(res2).to.be.an('array').empty;
580+
expect(res).to.be.an('array').empty;
574581
});
575582

576583
it('If the operation fail, the resolution should be an error', () => {
@@ -589,34 +596,40 @@ describe('delete', () => {
589596
expect(fn.delete('employees', '1')).to.be.an('object');
590597
});
591598

592-
it('If the operation is succesful, the result is an id or an array of ids', async () => {
593-
const resArray = await fn.delete('departments', ['5', '7'])
599+
it('If an array is passed to the operation, the result is an array of ids', async () => {
600+
const res = await fn.delete('departments', ['5', '7'])
594601
.promise();
595-
expect(resArray).to.be.an('array');
596-
expect(resArray).to.have.lengthOf(2);
597-
expect(resArray).to.have.members(['5', '7']);
598-
599-
const resObject = await fn.delete('departments', '8')
600-
.promise();
601-
expect(resObject).to.be.a('string').equals('8');
602+
expect(res).to.be.an('array');
603+
expect(res).to.have.lengthOf(2);
604+
expect(res).to.have.members(['5', '7']);
602605
});
603606

604-
it('If the operation is not starter, the operation delete the items on which we are operating', async () => {
605-
const res1 = await fn.table('departments', '9')
607+
it('If the operation is succesful, the result is an id', async () => {
608+
const res = await fn.delete('departments', '8')
609+
.promise();
610+
expect(res).to.be.a('string').equals('8');
611+
});
612+
613+
it('If the operation is not starter, and it\'s performed over a single element, the operation will delete that item', async () => {
614+
const res = await fn.table('departments', '9')
606615
.project('id')
607616
.delete()
608617
.promise();
609-
expect(res1).to.be.an('string').equal('9');
610-
611-
const res2 = await fn.table('departments', ['10', '11', '12'])
618+
expect(res).to.be.an('string').equal('9');
619+
});
620+
621+
it('If the operation is not starter, and it\'s performed over an array of elements, the operation will delete these items', async () => {
622+
const res = await fn.table('departments', ['10', '11', '12'])
612623
.project('id')
613624
.delete()
614625
.promise();
615-
expect(res2).to.be.an('array');
616-
expect(res2).to.have.lengthOf(3);
617-
expect(res2).to.have.members(['10', '11', '12']);
618-
619-
const res3 = await fn.table('departments', '13')
626+
expect(res).to.be.an('array');
627+
expect(res).to.have.lengthOf(3);
628+
expect(res).to.have.members(['10', '11', '12']);
629+
});
630+
631+
it('If the operation is not starter, it\'s possible to get the item to delete reducing the object which we want to delete', async () => {
632+
const res = await fn.table('departments', '13')
620633
.reduce((accum: string, o: string | number[], key: string) => {
621634
if (key === 'id')
622635
return o;
@@ -625,31 +638,35 @@ describe('delete', () => {
625638
}, '')
626639
.delete()
627640
.promise();
628-
expect(res3).to.be.an('string').equal('13');
629-
630-
const res4 = await fn.table('departments')
641+
expect(res).to.be.an('string').equal('13');
642+
});
643+
644+
it('If the operation is not starter, it\'s possible to get the items to delete reducing an array of objects', async () => {
645+
const res = await fn.table('departments')
631646
.reduce((accum: string[], o: Department) => {
632647
if (Number.parseInt(o.id) > 13)
633648
accum.push(o.id)
634649
return accum
635650
})
636651
.delete()
637652
.promise();
638-
expect(res4).to.be.an('array');
639-
expect(res4).to.have.lengthOf(2);
640-
expect(res4).to.have.members(['15', '16']);
653+
expect(res).to.be.an('array');
654+
expect(res).to.have.lengthOf(2);
655+
expect(res).to.have.members(['15', '16']);
641656
});
642657

643-
it('If the operation it\'s done over an empty array, the result should be an empty array', async () => {
644-
const res1 = await fn.delete('employees', [])
658+
it('If an empty array is passed to the operation, the result should be an empty array', async () => {
659+
const res = await fn.delete('employees', [])
645660
.promise();
646-
expect(res1).to.be.an('array').empty;
647-
648-
const res2 = await fn.table('employees')
661+
expect(res).to.be.an('array').empty;
662+
});
663+
664+
it('If the operation is done over an empty array, the result should be an empty array', async () => {
665+
const res = await fn.table('employees')
649666
.where('id', 'x')
650667
.delete()
651668
.promise();
652-
expect(res2).to.be.an('array').empty;
669+
expect(res).to.be.an('array').empty;
653670
});
654671

655672
it('If the operation is performed over an inexisten table, the resolution should be an error', () => {

test/fn-s3.ts renamed to test/adapters/fn-s3.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import { expect } from 'chai';
3-
import fn from '../src/index';
4-
import s3 from '../src/adapters/fn-s3';
3+
import fn from '../../src/index';
4+
import s3 from '../../src/adapters/fn-s3';
55
import * as AWS from 'aws-sdk';
66

77
let endpoint = process.env.ENDPOINT || 'http://localhost:4572/';
@@ -102,15 +102,17 @@ describe('deleteObject', function () {
102102
expect(fn.deleteObject('oasp4fn', 'test1.txt')).to.be.an('object');
103103
});
104104

105-
it('If the operation is succesfull, the resolution should be the key or keys of deleted object/s', async () => {
106-
const resObject = await fn.deleteObject('oasp4fn', 'test.txt').promise();
107-
expect(resObject).to.be.a('string');
108-
expect(resObject).to.be.equal('test.txt');
105+
it('If the operation is performed over a single object key, the resolution should be the key of the deleted object', async () => {
106+
const res = await fn.deleteObject('oasp4fn', 'test.txt').promise();
107+
expect(res).to.be.a('string');
108+
expect(res).to.be.equal('test.txt');
109+
});
109110

110-
const resArray = await fn.deleteObject('oasp4fn', ['test2.txt', 'test3.txt']).promise();
111-
expect(resArray).to.be.an('array');
112-
expect(resArray).to.have.lengthOf(2);
113-
expect(resArray).to.have.members(['test2.txt', 'test3.txt']);
111+
it('If the operation is over an array of object keys, the resolution should be the keys of deleted objects', async () => {
112+
const res = await fn.deleteObject('oasp4fn', ['test2.txt', 'test3.txt']).promise();
113+
expect(res).to.be.an('array');
114+
expect(res).to.have.lengthOf(2);
115+
expect(res).to.have.members(['test2.txt', 'test3.txt']);
114116
});
115117

116118
it('If the operation is not starter, the operation delete the objects on which we are operating', async () => {

0 commit comments

Comments
 (0)