Skip to content

Commit

Permalink
Merge pull request #11066 from microsoft/users/zarenner/apiKeyFix
Browse files Browse the repository at this point in the history
NuGetAuthenticate: Fix bug where wrong error message when specifying …
  • Loading branch information
zarenner authored Aug 5, 2019
2 parents 245f88d + c990b53 commit f515dad
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from "assert";
import * as mockery from "mockery";
import { EndpointAuthorization } from "azure-pipelines-task-lib";
import { ServiceConnectionAuthType, TokenServiceConnection, UsernamePasswordServiceConnection, IAdditionalData } from "../serviceConnectionUtils";
import { ServiceConnectionAuthType, TokenServiceConnection, UsernamePasswordServiceConnection, ApiKeyServiceConnection, IAdditionalData } from "../serviceConnectionUtils";

export function serviceConnectionUtilsTests() {

Expand All @@ -21,6 +21,7 @@ export function serviceConnectionUtilsTests() {

beforeEach(() => {
mockery.resetCache();
mockery.registerAllowable("../serviceConnectionUtils");
});

afterEach(() => {
Expand Down Expand Up @@ -55,6 +56,7 @@ export function serviceConnectionUtilsTests() {

it("getPackagingServiceConnections token good", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
Expand All @@ -80,6 +82,7 @@ export function serviceConnectionUtilsTests() {

it("getPackagingServiceConnections token missing apitoken throws", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
Expand All @@ -98,6 +101,7 @@ export function serviceConnectionUtilsTests() {

it("getPackagingServiceConnections username/password good", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
Expand All @@ -124,6 +128,7 @@ export function serviceConnectionUtilsTests() {

it("getPackagingServiceConnections username/password missing username throws", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
Expand Down Expand Up @@ -158,6 +163,52 @@ export function serviceConnectionUtilsTests() {
done();
});

it("getPackagingServiceConnections apikey good", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
getEndpointAuthorization: (key, optional) => <EndpointAuthorization>{
parameters: { "nugetkey": "someapikey" },
scheme: "none"
},
getEndpointAuthorizationScheme: (key, optional): string => "none"
};
mockery.registerMock('azure-pipelines-task-lib/task', mockTask);

let serviceConnectionUtilsWithMocks = require("../serviceConnectionUtils");
assert.deepEqual(serviceConnectionUtilsWithMocks.getPackagingServiceConnections(serviceConnectionsKey), [<ApiKeyServiceConnection>{
packageSource: {
uri: "https://contoso.com/nuget/v3/index.json"
},
authType: ServiceConnectionAuthType.ApiKey,
apiKey: "someapikey",
additionalData: {}
}]);
done();
});

it("getPackagingServiceConnections apikey missing nugetkey throws", (done: MochaDone) => {
let mockTask = {
loc: msg => msg,
debug: () => {},
getDelimitedInput: (key) => ["tokenendpoint1"],
getEndpointUrl: (key, optional) => "https://contoso.com/nuget/v3/index.json",
getEndpointAuthorization: (key, optional) => <EndpointAuthorization>{
parameters: { /* missing nugetkey */ },
scheme: "none"
},
getEndpointAuthorizationScheme: (key, optional): string => "none"
};
mockery.registerMock('azure-pipelines-task-lib/task', mockTask);

let serviceConnectionUtilsWithMocks = require("../serviceConnectionUtils");
assert.throws(() => serviceConnectionUtilsWithMocks.getPackagingServiceConnections(serviceConnectionsKey));
done();
});


it("getPackagingServiceConnections token good additional data good", (done: MochaDone) => {
let mockTask = {
debug: () => {},
Expand Down
2 changes: 1 addition & 1 deletion Tasks/Common/artifacts-common/serviceConnectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getPackagingServiceConnections(endpointsInputName: string, dataP
additionalData));
break;
case "none": // We only support this for nuget today. npm and python tasks do not use this endpoint auth scheme.
if ("nugetkey" in endpointAuth.parameters) {
if (!("nugetkey" in endpointAuth.parameters)) {
throw Error(tl.loc("ServiceConnections_Error_FailedToParseServiceEndpoint_MissingParameter", uri, "nugetkey"));
}

Expand Down
2 changes: 1 addition & 1 deletion Tasks/MavenAuthenticateV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 0,
"Minor": 156,
"Patch": 0
"Patch": 1
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/MavenAuthenticateV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 0,
"Minor": 156,
"Patch": 0
"Patch": 1
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/NuGetAuthenticateV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 156,
"Patch": 2
"Patch": 3
},
"minimumAgentVersion": "2.120.0",
"instanceNameFormat": "NuGet Authenticate",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/NuGetAuthenticateV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 156,
"Patch": 2
"Patch": 3
},
"minimumAgentVersion": "2.120.0",
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PipAuthenticateV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PipAuthenticateV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/TwineAuthenticateV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/TwineAuthenticateV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down

0 comments on commit f515dad

Please sign in to comment.