|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const crypto = require('crypto'); |
3 | 4 | const BbPromise = require('bluebird');
|
4 | 5 | const expect = require('chai').expect;
|
5 | 6 | const chaiAsPromised = require('chai-as-promised');
|
@@ -46,11 +47,29 @@ describe('OpenWhiskCompileHttpEvents', () => {
|
46 | 47 | c: { events: [ { http: true } ], annotations: { 'web-export': false } },
|
47 | 48 | d: { events: [ { http: true } ] }
|
48 | 49 | }
|
| 50 | + |
| 51 | + const auth_string = crypto.randomBytes(64).toString('hex'); |
| 52 | + sandbox.stub(openwhiskCompileHttpEvents, 'generateAuthString', () => auth_string); |
| 53 | + |
| 54 | + return openwhiskCompileHttpEvents.addWebAnnotations().then(() => { |
| 55 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.a.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': auth_string }) |
| 56 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.b.annotations).to.deep.equal({ foo: 'bar', 'web-export': true, 'require-whisk-auth': auth_string }) |
| 57 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.c.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': auth_string }) |
| 58 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.d.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': auth_string }) |
| 59 | + }) |
| 60 | + }); |
| 61 | + |
| 62 | + it('should not add auth annotation when annotation already present', () => { |
| 63 | + openwhiskCompileHttpEvents.serverless.service.functions = { |
| 64 | + a: { events: [ { http: true } ], annotations: { 'require-whisk-auth': false } }, |
| 65 | + b: { events: [ { http: true } ], annotations: { 'require-whisk-auth': true } }, |
| 66 | + c: { events: [ { http: true } ], annotations: { 'require-whisk-auth': 'some string' } } |
| 67 | + } |
| 68 | + |
49 | 69 | return openwhiskCompileHttpEvents.addWebAnnotations().then(() => {
|
50 |
| - expect(openwhiskCompileHttpEvents.serverless.service.functions.a.annotations).to.deep.equal({ 'web-export': true }) |
51 |
| - expect(openwhiskCompileHttpEvents.serverless.service.functions.b.annotations).to.deep.equal({ foo: 'bar', 'web-export': true }) |
52 |
| - expect(openwhiskCompileHttpEvents.serverless.service.functions.c.annotations).to.deep.equal({ 'web-export': true }) |
53 |
| - expect(openwhiskCompileHttpEvents.serverless.service.functions.d.annotations).to.deep.equal({ 'web-export': true }) |
| 70 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.a.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': false }) |
| 71 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.b.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': true }) |
| 72 | + expect(openwhiskCompileHttpEvents.serverless.service.functions.c.annotations).to.deep.equal({ 'web-export': true, 'require-whisk-auth': 'some string' }) |
54 | 73 | })
|
55 | 74 | });
|
56 | 75 |
|
@@ -152,6 +171,16 @@ describe('OpenWhiskCompileHttpEvents', () => {
|
152 | 171 | return expect(result).to.deep.equal({basepath: '/my-service', relpath: '/api/foo/bar', operation: 'GET', action: '/sample_ns/my-service_action-name', responsetype: 'json'});
|
153 | 172 | });
|
154 | 173 |
|
| 174 | + it('should add secure auth key if present', () => { |
| 175 | + openwhiskCompileHttpEvents.serverless.service.service = 'my-service' |
| 176 | + openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"}; |
| 177 | + const http = {path: "/api/foo/bar", method: "GET"} |
| 178 | + const result = openwhiskCompileHttpEvents.compileHttpEvent('action-name', { |
| 179 | + annotations: { 'require-whisk-auth': 'auth-token' } |
| 180 | + }, http); |
| 181 | + return expect(result).to.deep.equal({basepath: '/my-service', relpath: '/api/foo/bar', operation: 'GET', secure_key: 'auth-token', action: '/sample_ns/my-service_action-name', responsetype: 'json'}); |
| 182 | + }); |
| 183 | + |
155 | 184 | it('should define http events with explicit response type', () => {
|
156 | 185 | openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
|
157 | 186 | openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
|
|
0 commit comments