Skip to content

Commit

Permalink
feat: add validation to broker token usage to avoid confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Fellows committed Jan 30, 2020
1 parent 13cbdc7 commit 2a4afa3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/publisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,32 @@ describe('Publish Spec', () => {
expect(p.publish).to.be.a('function');
});
});

context('when a bearer token is provided', () => {
context("and specifies a username or password", () => {
it('should fail with an error', () => {
expect(() =>
publisherFactory({
pactBroker: 'http://localhost',
pactFilesOrDirs: [relativePath],
consumerVersion: '1.0.0',
pactBrokerToken: "1234",
pactBrokerUsername: "username",
pactBrokerPassword: "5678",
})
).to.throw(Error)
});

});
it('should not fail', () => {
const p = publisherFactory({
pactBroker: 'http://localhost',
pactFilesOrDirs: [pactFile],
consumerVersion: '1.0.0',
pactBrokerToken: "1234"
});
expect(p).to.be.ok;
expect(p.publish).to.be.a('function');
});
});
});
6 changes: 6 additions & 0 deletions src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export class Publisher {
);
}

if (options.pactBrokerToken && (options.pactBrokerUsername || options.pactBrokerPassword)) {
throw new Error(
'Must provide pactBrokerToken or pactBrokerUsername/pactBrokerPassword but not both.',
);
}

this.options = options;
}

Expand Down
25 changes: 25 additions & 0 deletions src/verifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,29 @@ describe('Verifier Spec', () => {
).to.not.throw(Error);
});
});

context('when using a bearer token', () => {
context('and specifies a username or password', () => {
it('should fail with an error', () => {
expect(() =>
verifierFactory({
providerBaseUrl: 'http://localhost',
pactUrls: ['http://idontexist'],
pactBrokerToken: "1234",
pactBrokerUsername: "username",
pactBrokerPassword: "5678",
}),
).to.throw(Error);
});
});
it('should not fail', () => {
const v = verifierFactory({
providerBaseUrl: 'http://localhost',
pactUrls: ['http://idontexist'],
pactBrokerToken: "1234",
})

expect(v.options.pactBrokerToken).to.eq("1234");
});
});
});
6 changes: 6 additions & 0 deletions src/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ export class Verifier {
checkTypes.assert.string(options.pactBrokerPassword);
}

if (options.pactBrokerToken && (options.pactBrokerUsername || options.pactBrokerPassword)) {
throw new Error(
'Must provide pactBrokerToken or pactBrokerUsername/pactBrokerPassword but not both.',
);
}

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

0 comments on commit 2a4afa3

Please sign in to comment.