Skip to content

Commit

Permalink
feat(verify): support broker bearer token
Browse files Browse the repository at this point in the history
- Upgrade standalone to 1.64.0
- The CLI verifier has all of the logic needed to fetch pacts
  from the Broker. This change removes the use of the Broker
  class to fetch Pacts
- Broker class removed as not required for public consumption
- Adds support for bearer token authentication mode
  • Loading branch information
mefellows committed Mar 7, 2019
1 parent 2032ba2 commit f060b78
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 260 deletions.
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,36 @@ An idiomatic Node interface for the [Pact](http://pact.io) mock service (Consume

<!-- TOC -->

- [Pact Node](#pact-node)
- [Installation](#installation)
- [Do Not Track](#do-not-track)
- [Pact Download Location](#pact-download-location)
- [Usage](#usage)
- [Documentation](#documentation)
- [Set Log Level](#set-log-level)
- [Mock Servers](#mock-servers)
- [Create Mock Server](#create-mock-server)
- [List Mock Servers](#list-mock-servers)
- [Remove All Mock Servers](#remove-all-mock-servers)
- [Start a Mock Server](#start-a-mock-server)
- [Stop a Mock server](#stop-a-mock-server)
- [Delete a Mock server](#delete-a-mock-server)
- [Check if a Mock server is running](#check-if-a-mock-server-is-running)
- [Mock Server Events](#mock-server-events)
- [Provider Verification](#provider-verification)
- [Pact Broker Publishing](#pact-broker-publishing)
- [Stub Servers](#stub-servers)
- [Create Stub Server](#create-stub-server)
- [Message Pacts](#message-pacts)
- [Create Message Pacts](#create-message-pacts)
- [Example](#example)
- [Example CLI invocation:](#example-cli-invocation)
- [Windows Issues](#windows-issues)
- [Contributing](#contributing)
- [Testing](#testing)
- [Questions?](#questions)
- [Pact Node](#pact-node)
- [Installation](#installation)
- [Do Not Track](#do-not-track)
- [Pact Download Location](#pact-download-location)
- [Usage](#usage)
- [Documentation](#documentation)
- [Set Log Level](#set-log-level)
- [Mock Servers](#mock-servers)
- [Create Mock Server](#create-mock-server)
- [List Mock Servers](#list-mock-servers)
- [Remove All Mock Servers](#remove-all-mock-servers)
- [Start a Mock Server](#start-a-mock-server)
- [Stop a Mock server](#stop-a-mock-server)
- [Delete a Mock server](#delete-a-mock-server)
- [Check if a Mock server is running](#check-if-a-mock-server-is-running)
- [Mock Server Events](#mock-server-events)
- [Provider Verification](#provider-verification)
- [Pact Broker Publishing](#pact-broker-publishing)
- [Pact Broker Deployment Check](#pact-broker-deployment-check)
- [Stub Servers](#stub-servers)
- [Create Stub Server](#create-stub-server)
- [Message Pacts](#message-pacts)
- [Create Message Pacts](#create-message-pacts)
- [Example](#example)
- [Example CLI invocation:](#example-cli-invocation)
- [Windows Issues](#windows-issues)
- [Enable Long Paths](#enable-long-paths)
- [Contributing](#contributing)
- [Testing](#testing)
- [Questions?](#questions)

<!-- /TOC -->

Expand Down Expand Up @@ -251,8 +253,7 @@ pact.verifyPacts({
| Parameter | Required? | Type | Description |
| --------------------------- | --------- | ------- | ---------------------------------------------------------------------------------------------------------- |
| `providerBaseUrl` | true | string | Running API provider host endpoint. |
| `pactBrokerBaseUrl` | false | string | Base URL of the Pact Broker from which to retrieve the pacts. |
| `pactBrokerUrl` | false | string | URL of your Pact Broker to dynamically discover relevent pacts to verify. Required if `pactUrls` not given |
| `pactBrokerUrl` | false | string | Base URL of the Pact Broker from which to retrieve the pacts. Required if `pactUrls` not given. |
| `provider` | false | string | Name of the provider if fetching from a Broker |
| `tags` | false | array | Array of tags, used to filter pacts from the Broker |
| `consumerVersionTag` | false | string | Retrieve the latest pacts with this consumer version tag |
Expand Down
93 changes: 0 additions & 93 deletions src/broker.spec.ts

This file was deleted.

94 changes: 0 additions & 94 deletions src/broker.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ export * from "./server";

export * from "./publisher";

export * from "./broker";

export * from "./stub";
63 changes: 23 additions & 40 deletions src/verifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path = require("path");
import url = require("url");
import brokerFactory, {BrokerOptions} from "./broker";
import logger from "./logger";
import pactUtil, {DEFAULT_ARG, SpawnArguments} from "./pact-util";
import q = require("q");
Expand All @@ -21,13 +20,15 @@ export class Verifier {
private readonly __argMapping = {
"pactUrls": DEFAULT_ARG,
"providerBaseUrl": "--provider-base-url",
"pactBrokerBaseUrl": "--pact-broker-base-url",
"pactBrokerUrl": "--pact-broker-base-url",
"providerStatesSetupUrl": "--provider-states-setup-url",
"pactBrokerUsername": "--broker-username",
"pactBrokerPassword": "--broker-password",
"pactBrokerToken": "--broker-token",
"consumerVersionTag": "--consumer-version-tag",
"publishVerificationResult": "--publish-verification-results",
"providerVersion": "--provider-app-version",
"provider": "--provider",
"customProviderHeaders": "--custom-provider-header",
"format": "--format",
"out": "--out",
Expand All @@ -36,7 +37,6 @@ export class Verifier {
constructor(options: VerifierOptions) {
options = options || {};
options.pactBrokerUrl = options.pactBrokerUrl || "";
options.pactBrokerBaseUrl = options.pactBrokerBaseUrl || "";
options.consumerVersionTag = options.consumerVersionTag || "";
options.tags = options.tags || [];
options.pactUrls = options.pactUrls || [];
Expand Down Expand Up @@ -69,11 +69,11 @@ export class Verifier {
checkTypes.assert.nonEmptyString(options.providerBaseUrl, "Must provide the providerBaseUrl argument");

if (checkTypes.emptyArray(options.pactUrls) && !options.pactBrokerUrl) {
throw new Error("Must provide the pactUrls argument if no brokerUrl provided");
throw new Error("Must provide the pactUrls argument if no pactBrokerUrl provided");
}

if ((!options.pactBrokerUrl || _.isEmpty(options.provider)) && checkTypes.emptyArray(options.pactUrls)) {
throw new Error("Must provide both provider and brokerUrl or if pactUrls not provided.");
if (( !options.pactBrokerUrl || _.isEmpty(options.provider)) && checkTypes.emptyArray(options.pactUrls)) {
throw new Error("Must provide both provider and pactBrokerUrl if pactUrls not provided.");
}

if (options.providerStatesSetupUrl) {
Expand All @@ -88,8 +88,8 @@ export class Verifier {
checkTypes.assert.string(options.pactBrokerPassword);
}

if (options.pactBrokerBaseUrl) {
checkTypes.assert.string(options.pactBrokerBaseUrl);
if (options.pactBrokerUrl) {
checkTypes.assert.string(options.pactBrokerUrl);
}

if (options.consumerVersionTag) {
Expand Down Expand Up @@ -146,36 +146,19 @@ export class Verifier {

public verify(): q.Promise<string> {
logger.info("Verifying Pact Files");
return q(this.options.pactUrls)
// TODO: fix this promise type issue by using regular old es6 promises, remove Q
.then((uris): any => {
if (!uris || uris.length === 0) {
return brokerFactory({
brokerUrl: this.options.pactBrokerUrl,
provider: this.options.provider,
username: this.options.pactBrokerUsername,
password: this.options.pactBrokerPassword,
tags: this.options.tags
} as BrokerOptions).findConsumers();
}
return uris;
})
.then((data: string[]): PromiseLike<string> => {
const deferred = q.defer<string>();
this.options.pactUrls = data;
const instance = pactUtil.spawnBinary(pactStandalone.verifierPath, this.options, this.__argMapping);
const output: any[] = [];
instance.stdout.on("data", (l) => output.push(l));
instance.stderr.on("data", (l) => output.push(l));
instance.once("close", (code) => {
const o = output.join("\n");
code === 0 ? deferred.resolve(o) : deferred.reject(new Error(o));
});

return deferred.promise
.timeout(this.options.timeout as number, `Timeout waiting for verification process to complete (PID: ${instance.pid})`)
.tap(() => logger.info("Pact Verification succeeded.")) as PromiseLike<string>;
});
const deferred = q.defer<string>();
const instance = pactUtil.spawnBinary(pactStandalone.verifierPath, this.options, this.__argMapping);
const output: any[] = [];
instance.stdout.on("data", (l) => output.push(l));
instance.stderr.on("data", (l) => output.push(l));
instance.once("close", (code) => {
const o = output.join("\n");
code === 0 ? deferred.resolve(o) : deferred.reject(new Error(o));
});

return deferred.promise
.timeout(this.options.timeout as number, `Timeout waiting for verification process to complete (PID: ${instance.pid})`)
.tap(() => logger.info("Pact Verification succeeded."));
}
}

Expand All @@ -186,15 +169,15 @@ export interface VerifierOptions extends SpawnArguments {
providerBaseUrl: string;
provider?: string;
pactUrls?: string[];
pactBrokerBaseUrl?: string;
pactBrokerUrl?: string;
providerStatesSetupUrl?: string;
pactBrokerUsername?: string;
pactBrokerPassword?: string;
pactBrokerToken?: string;
consumerVersionTag?: string;
customProviderHeaders?: string[];
publishVerificationResult?: boolean;
providerVersion?: string;
pactBrokerUrl?: string;
tags?: string[];
timeout?: number;
monkeypatch?: string;
Expand Down
Loading

0 comments on commit f060b78

Please sign in to comment.