Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ os: linux

matrix:
include:
- node_js: '6'
- node_js: '8'
- node_js: '10'

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"eslint": "^5.16.0",
"jest": "^24.8.0",
"jest-cli": "^24.8.0",
"mock-fs": "^4.10.0",
"serverless": "^1.43.0",
"ts-jest": "^24.0.2",
"typescript": "^3.4.5"
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/login/loginPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("Login Plugin", () => {
AzureLoginService.servicePrincipalLogin = servicePrincipalLogin;

const sls = MockFactory.createTestServerless();
delete sls.variables['azureCredentials']
const options = MockFactory.createTestServerlessOptions();
const plugin = new AzureLoginPlugin(sls, options);

Expand All @@ -56,6 +57,7 @@ describe("Login Plugin", () => {
AzureLoginService.servicePrincipalLogin = servicePrincipalLogin;

const sls = MockFactory.createTestServerless();
delete sls.variables['azureCredentials']
const options = MockFactory.createTestServerlessOptions();
const plugin = new AzureLoginPlugin(sls, options);
await invokeHook(plugin, "before:package:initialize");
Expand Down Expand Up @@ -83,6 +85,7 @@ describe("Login Plugin", () => {
AzureLoginService.servicePrincipalLogin = servicePrincipalLogin;

const sls = MockFactory.createTestServerless();
delete sls.variables['azureCredentials']
const options = MockFactory.createTestServerlessOptions();
const plugin = new AzureLoginPlugin(sls, options);
await invokeHook(plugin, "before:package:initialize");
Expand All @@ -109,6 +112,7 @@ describe("Login Plugin", () => {
AzureLoginService.servicePrincipalLogin = servicePrincipalLogin;

const sls = MockFactory.createTestServerless();
delete sls.variables['azureCredentials']
const options = MockFactory.createTestServerlessOptions();
const plugin = new AzureLoginPlugin(sls, options);
await invokeHook(plugin, "before:package:initialize");
Expand Down
36 changes: 36 additions & 0 deletions src/services/loginService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AzureLoginService } from "./loginService";
jest.mock("open");
import open from "open";

jest.mock("@azure/ms-rest-nodeauth")
import { interactiveLoginWithAuthResponse, loginWithServicePrincipalSecretWithAuthResponse } from "@azure/ms-rest-nodeauth";

describe('Login Service', () => {

it('logs in interactively', async () => {
// Ensure env variables are not set
delete process.env.azureSubId;
delete process.env.azureServicePrincipalClientId;
delete process.env.azureServicePrincipalPassword;
delete process.env.azureServicePrincipalTenantId;

await AzureLoginService.login();
expect(open).toBeCalledWith("https://microsoft.com/devicelogin")
expect(interactiveLoginWithAuthResponse).toBeCalled();
});

it('logs in with a service principal', async () => {
// Set environment variables
process.env.azureSubId = 'azureSubId';
process.env.azureServicePrincipalClientId = 'azureServicePrincipalClientId';
process.env.azureServicePrincipalPassword = 'azureServicePrincipalPassword';
process.env.azureServicePrincipalTenantId = 'azureServicePrincipalTenantId';

await AzureLoginService.login();
expect(loginWithServicePrincipalSecretWithAuthResponse).toBeCalledWith(
'azureServicePrincipalClientId',
'azureServicePrincipalPassword',
'azureServicePrincipalTenantId'
);
});
});
1 change: 1 addition & 0 deletions src/services/loginService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { interactiveLoginWithAuthResponse, loginWithServicePrincipalSecretWithAuthResponse } from "@azure/ms-rest-nodeauth";
import open from "open";

export class AzureLoginService {

Expand Down
73 changes: 73 additions & 0 deletions src/services/resourceService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { MockFactory } from '../test/mockFactory';
import { ResourceService } from './resourceService';


jest.mock('@azure/arm-resources')
import { ResourceManagementClient } from '@azure/arm-resources';

describe('Resource Service', () => {

beforeAll(() => {
ResourceManagementClient.prototype.resourceGroups = {
createOrUpdate: jest.fn(),
deleteMethod: jest.fn(),
} as any;

ResourceManagementClient.prototype.deployments = {
deleteMethod: jest.fn()
} as any;
});

it('throws error with empty credentials', () => {
const sls = MockFactory.createTestServerless();
delete sls.variables['azureCredentials']
const options = MockFactory.createTestServerlessOptions();
expect(() => new ResourceService(sls, options)).toThrowError('Azure Credentials has not been set in ResourceService')
});

it('initializes a resource service', () => {
const sls = MockFactory.createTestServerless();
const options = MockFactory.createTestServerlessOptions();
expect(() => new ResourceService(sls, options)).not.toThrowError();
});

it('deploys a resource group', () => {
const sls = MockFactory.createTestServerless();
const resourceGroup = 'myResourceGroup'
const location = 'West Us';
sls.service.provider['resourceGroup'] = resourceGroup
sls.service.provider['location'] = location;
sls.variables['azureCredentials'] = 'fake credentials'
const options = MockFactory.createTestServerlessOptions();
const service = new ResourceService(sls, options);
service.deployResourceGroup();
expect(ResourceManagementClient.prototype.resourceGroups.createOrUpdate)
.toBeCalledWith(resourceGroup, { location });
});

it('deletes a deployment', () => {
const sls = MockFactory.createTestServerless();
const resourceGroup = 'myResourceGroup';
const deploymentName = 'myDeployment';
sls.service.provider['resourceGroup'] = resourceGroup
sls.service.provider['deploymentName'] = deploymentName;
sls.variables['azureCredentials'] = 'fake credentials'
const options = MockFactory.createTestServerlessOptions();
const service = new ResourceService(sls, options);
service.deleteDeployment();
expect(ResourceManagementClient.prototype.deployments.deleteMethod)
.toBeCalledWith(resourceGroup, deploymentName);
});

it('deletes a resource group', () => {
const sls = MockFactory.createTestServerless();
const resourceGroup = 'myResourceGroup';
sls.service.provider['resourceGroup'] = resourceGroup
sls.variables['azureCredentials'] = 'fake credentials'
const options = MockFactory.createTestServerlessOptions();
const service = new ResourceService(sls, options);
service.deleteResourceGroup();
expect(ResourceManagementClient.prototype.resourceGroups.deleteMethod)
.toBeCalledWith(resourceGroup);
});
});
51 changes: 43 additions & 8 deletions src/test/mockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import Service from "serverless/classes/Service";
import Utils = require("serverless/classes/Utils");
import PluginManager = require("serverless/classes/PluginManager");

function getAttribute(object: any, prop: string, defaultValue: any): any {
if (object && object[prop]) {
return object[prop];
}
return defaultValue;
}

export class MockFactory {
public static createTestServerless(config?: any): Serverless {
const sls = new Serverless(config);
sls.service = MockFactory.createTestService();
sls.utils = MockFactory.createTestUtils();
sls.cli = MockFactory.createTestCli();
sls.pluginManager = MockFactory.createTestPluginManager();
sls.variables = {};
sls.service = getAttribute(config, "service", MockFactory.createTestService());
sls.utils = getAttribute(config, "utils", MockFactory.createTestUtils());
sls.cli = getAttribute(config, "cli", MockFactory.createTestCli());
sls.pluginManager = getAttribute(config, "pluginManager", MockFactory.createTestPluginManager());
sls.variables = getAttribute(config, "variables", MockFactory.createTestVariables());
return sls;
}

Expand All @@ -37,7 +44,15 @@ export class MockFactory {
}
}

private static createTestService(): Service {
public static createTestFunctionApp() {
return {
id: "App Id",
name: "App Name",
defaultHostName: "My Host Name"
}
}

public static createTestService(): Service {
return {
getAllFunctions: jest.fn(() => ["function1"]),
getFunction: jest.fn(),
Expand All @@ -51,8 +66,28 @@ export class MockFactory {
update: jest.fn(),
validate: jest.fn(),
custom: null,
provider: {} as any,
};
provider: MockFactory.createTestAzureServiceProvider(),
service: "serviceName",
artifact: "app.zip",
} as any as Service;
}

public static createTestAzureServiceProvider() {
return {
resourceGroup: "myResourceGroup",
deploymentName: "myDeploymentName",
}
}

public static createTestVariables() {
return {
azureCredentials: "credentials",
subscriptionId: "subId",
}
}

private getConfig(config: any, prop: string, defaultValue: any) {

}

private static createTestUtils(): Utils {
Expand Down