mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Merge pull request #4103 from serverless/platform-gate
Add platform gate to sls run/emit
This commit is contained in:
commit
caba4cc89a
@ -5,6 +5,7 @@ const BbPromise = require('bluebird');
|
||||
const fdk = require('@serverless/fdk');
|
||||
const path = require('path');
|
||||
const stdin = require('get-stdin');
|
||||
const getAuthToken = require('../../utils/getAuthToken');
|
||||
const userStats = require('../../utils/userStats');
|
||||
const chalk = require('chalk');
|
||||
|
||||
@ -61,7 +62,10 @@ class Emit {
|
||||
|
||||
this.hooks = {
|
||||
'emit:emit': () =>
|
||||
BbPromise.bind(this).then(this.retrieveData).then(this.parseData).then(this.emitEvent),
|
||||
BbPromise.bind(this)
|
||||
.then(this.retrieveData)
|
||||
.then(this.parseData)
|
||||
.then(this.emitEvent),
|
||||
};
|
||||
}
|
||||
|
||||
@ -116,6 +120,12 @@ class Emit {
|
||||
}
|
||||
|
||||
emitEvent() {
|
||||
const authToken = getAuthToken();
|
||||
if (!authToken) {
|
||||
return BbPromise.reject(new this.serverless.classes
|
||||
.Error('Must be logged in to use this command. Please run "serverless login".'));
|
||||
}
|
||||
|
||||
userStats.track('service_emitted');
|
||||
const url = this.options.url || 'http://localhost:4000';
|
||||
const eventGateway = fdk.eventGateway({
|
||||
|
||||
@ -13,27 +13,25 @@ chai.use(require('chai-as-promised'));
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('Emit', () => {
|
||||
let emit;
|
||||
let serverless;
|
||||
let emitEventStub;
|
||||
let logStub;
|
||||
|
||||
beforeEach(() => {
|
||||
serverless = new Serverless();
|
||||
serverless.cli = new CLI(serverless);
|
||||
emitEventStub = sinon.stub().resolves();
|
||||
const Emit = proxyquire('./index', {
|
||||
'@serverless/fdk': {
|
||||
eventGateway: () => ({
|
||||
emit: emitEventStub,
|
||||
}),
|
||||
},
|
||||
});
|
||||
emit = new Emit(serverless);
|
||||
logStub = sinon.stub(emit.serverless.cli, 'consoleLog');
|
||||
});
|
||||
|
||||
describe('#constructor()', () => {
|
||||
let emit;
|
||||
let serverless;
|
||||
let emitEventStub;
|
||||
|
||||
beforeEach(() => {
|
||||
serverless = new Serverless();
|
||||
serverless.cli = new CLI(serverless);
|
||||
emitEventStub = sinon.stub().resolves();
|
||||
const Emit = proxyquire('./index', {
|
||||
'@serverless/fdk': {
|
||||
eventGateway: () => ({
|
||||
emit: emitEventStub,
|
||||
}),
|
||||
},
|
||||
});
|
||||
emit = new Emit(serverless);
|
||||
});
|
||||
|
||||
it('should have commands', () => expect(emit.commands).to.be.not.empty);
|
||||
it('should have hooks', () => expect(emit.hooks).to.be.not.empty);
|
||||
|
||||
@ -52,6 +50,24 @@ describe('Emit', () => {
|
||||
});
|
||||
|
||||
describe('#retrieveData()', () => {
|
||||
let emit;
|
||||
let serverless;
|
||||
let emitEventStub;
|
||||
|
||||
beforeEach(() => {
|
||||
serverless = new Serverless();
|
||||
serverless.cli = new CLI(serverless);
|
||||
emitEventStub = sinon.stub().resolves();
|
||||
const Emit = proxyquire('./index', {
|
||||
'@serverless/fdk': {
|
||||
eventGateway: () => ({
|
||||
emit: emitEventStub,
|
||||
}),
|
||||
},
|
||||
});
|
||||
emit = new Emit(serverless);
|
||||
});
|
||||
|
||||
it('should use the data args if provided over path', () => {
|
||||
emit.options.path = '/some/path';
|
||||
emit.options.data = '{"key": "value"}';
|
||||
@ -129,7 +145,27 @@ describe('Emit', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#emitEvent()', () => {
|
||||
describe('#emitEvent() - logged in', () => {
|
||||
let emit;
|
||||
let serverless;
|
||||
let emitEventStub;
|
||||
let logStub;
|
||||
|
||||
beforeEach(() => {
|
||||
serverless = new Serverless();
|
||||
serverless.cli = new CLI(serverless);
|
||||
emitEventStub = sinon.stub().resolves();
|
||||
const Emit = proxyquire('./index', {
|
||||
'@serverless/fdk': {
|
||||
eventGateway: () => ({
|
||||
emit: emitEventStub,
|
||||
}),
|
||||
},
|
||||
'../../utils/getAuthToken': () => 'abc123',
|
||||
});
|
||||
emit = new Emit(serverless);
|
||||
logStub = sinon.stub(emit.serverless.cli, 'consoleLog');
|
||||
});
|
||||
it('should emit an event using the name args', () => {
|
||||
emit.options.name = 'userCreated';
|
||||
emit.data = { key: 'value' };
|
||||
@ -168,4 +204,36 @@ describe('Emit', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#emitEvent() - logged out', () => {
|
||||
let emit;
|
||||
let serverless;
|
||||
let emitEventStub;
|
||||
|
||||
beforeEach(() => {
|
||||
serverless = new Serverless();
|
||||
serverless.cli = new CLI(serverless);
|
||||
emitEventStub = sinon.stub().resolves();
|
||||
const Emit = proxyquire('./index', {
|
||||
'@serverless/fdk': {
|
||||
eventGateway: () => ({
|
||||
emit: emitEventStub,
|
||||
}),
|
||||
},
|
||||
'../../utils/getAuthToken': () => null,
|
||||
});
|
||||
emit = new Emit(serverless);
|
||||
});
|
||||
|
||||
it('should throw an Error of not logged in', () => {
|
||||
emit.options.name = 'userCreated';
|
||||
emit.data = { key: 'value' };
|
||||
return emit.emitEvent().catch(err => {
|
||||
expect(err).to.be.an.instanceOf(Error);
|
||||
expect(err.message).to.equal(
|
||||
'Must be logged in to use this command. Please run "serverless login".'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -10,9 +10,9 @@ const BbPromise = require('bluebird');
|
||||
const fsExtra = require('../../utils/fs/fse');
|
||||
const fetch = require('node-fetch');
|
||||
const chalk = require('chalk');
|
||||
const configUtils = require('../../utils/config');
|
||||
const functionInfoUtils = require('../../utils/functionInfoUtils');
|
||||
const createApolloClient = require('../../utils/createApolloClient');
|
||||
const getAuthToken = require('../../utils/getAuthToken');
|
||||
const selectServicePublish = require('../../utils/selectors/selectServicePublish');
|
||||
|
||||
// NOTE Needed for apollo to work
|
||||
@ -91,17 +91,7 @@ class Platform {
|
||||
}
|
||||
|
||||
getAuthToken() {
|
||||
if (process.env.SERVERLESS_TOKEN) {
|
||||
return process.env.SERVERLESS_TOKEN;
|
||||
}
|
||||
|
||||
const userConfig = configUtils.getConfig();
|
||||
const currentId = userConfig.userId;
|
||||
const globalConfig = configUtils.getGlobalConfig();
|
||||
if (globalConfig.users && globalConfig.users[currentId] && globalConfig.users[currentId].auth) {
|
||||
return globalConfig.users[currentId].auth.id_token;
|
||||
}
|
||||
return null;
|
||||
return getAuthToken();
|
||||
}
|
||||
|
||||
publishService() {
|
||||
|
||||
@ -20,6 +20,8 @@ const registerFunctionsToEventGateway = require('./utils/registerFunctionsToEven
|
||||
const manageLocalEmulator = require('./utils/manageLocalEmulator');
|
||||
const manageEventGateway = require('./utils/manageEventGateway');
|
||||
|
||||
const getAuthToken = require('../../utils/getAuthToken');
|
||||
|
||||
class Run {
|
||||
constructor(serverless, options) {
|
||||
this.serverless = serverless;
|
||||
@ -65,6 +67,12 @@ class Run {
|
||||
const EVENT_GATEWAY_VERSION = '0.5.14';
|
||||
const LOCAL_EMULATOR_VERSION = '0.1.18';
|
||||
|
||||
const authToken = getAuthToken();
|
||||
if (!authToken) {
|
||||
throw new this.serverless.classes
|
||||
.Error('Must be logged in to use this command. Please run "serverless login".');
|
||||
}
|
||||
|
||||
let functionsDeployed = false;
|
||||
let functionsRegistered = false;
|
||||
if (!this.serverless.config.servicePath) {
|
||||
|
||||
19
lib/utils/getAuthToken.js
Normal file
19
lib/utils/getAuthToken.js
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const configUtils = require('./config');
|
||||
|
||||
function getAuthToken() {
|
||||
if (process.env.SERVERLESS_TOKEN) {
|
||||
return process.env.SERVERLESS_TOKEN;
|
||||
}
|
||||
|
||||
const userConfig = configUtils.getConfig();
|
||||
const currentId = userConfig.userId;
|
||||
const globalConfig = configUtils.getGlobalConfig();
|
||||
if (globalConfig.users && globalConfig.users[currentId] && globalConfig.users[currentId].auth) {
|
||||
return globalConfig.users[currentId].auth.id_token;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = getAuthToken;
|
||||
Loading…
x
Reference in New Issue
Block a user