Skip to content

Commit

Permalink
Use args object for getLoginCredential()
Browse files Browse the repository at this point in the history
  • Loading branch information
lfkellogg committed Feb 6, 2024
1 parent 8e10ed4 commit c1f6d9e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
21 changes: 13 additions & 8 deletions src/appdistribution/options-parser-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ function parseTestDevice(testDeviceString: string): TestDevice {
* Takes option values for username and password related options and returns a LoginCredential
* object that can be passed to the API.
*/
export function getLoginCredential(
username?: string,
password?: string,
passwordFile?: string,
usernameResourceName?: string,
passwordResourceName?: string,
): LoginCredential | undefined {
export function getLoginCredential(args: {
username?: string;
password?: string;
passwordFile?: string;
usernameResourceName?: string;
passwordResourceName?: string;
}): LoginCredential | undefined {
const { username, passwordFile, usernameResourceName, passwordResourceName } = args;
let password = args.password;
if (!password && passwordFile) {
ensureFileExists(passwordFile);
password = fs.readFileSync(passwordFile, "utf8").trim();
Expand All @@ -148,7 +150,10 @@ export function getLoginCredential(
}
let fieldHints: FieldHints | undefined;
if (usernameResourceName && passwordResourceName) {
fieldHints = { usernameResourceName, passwordResourceName };
fieldHints = {
usernameResourceName: usernameResourceName,
passwordResourceName: passwordResourceName,
};
}

if (isPresenceMismatched(username, password)) {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/appdistribution-distribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export const command = new Command("appdistribution:distribute <release-binary-f
const testers = getTestersOrGroups(options.testers, options.testersFile);
const groups = getTestersOrGroups(options.groups, options.groupsFile);
const testDevices = getTestDevices(options.testDevices, options.testDevicesFile);
const loginCredential = getLoginCredential(
options.testUsername,
options.testPassword,
options.testPasswordFile,
options.testUsernameResource,
options.testPasswordResource,
);
const loginCredential = getLoginCredential({
username: options.testUsername,
password: options.testPassword,
passwordFile: options.testPasswordFile,
usernameResourceName: options.testUsernameResource,
passwordResourceName: options.testPasswordResource,
});
const requests = new AppDistributionClient();
let aabInfo: AabInfo | undefined;

Expand Down
59 changes: 31 additions & 28 deletions src/test/appdistro/options-parser-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("options-parser-util", () => {

describe("getLoginCredential", () => {
it("returns credential for username and password", () => {
const result = getLoginCredential("user", "123");
const result = getLoginCredential({ username: "user", password: "123" });

expect(result).to.deep.equal({
username: "user",
Expand All @@ -109,7 +109,7 @@ describe("options-parser-util", () => {
});

it("returns credential for username and passwordFile", () => {
const result = getLoginCredential("user", undefined, passwordFile);
const result = getLoginCredential({ username: "user", passwordFile });

expect(result).to.deep.equal({
username: "user",
Expand All @@ -119,19 +119,18 @@ describe("options-parser-util", () => {
});

it("returns undefined when no options provided", () => {
const result = getLoginCredential();
const result = getLoginCredential({});

expect(result).to.be.undefined;
});

it("returns credential for username, password, and resource names", () => {
const result = getLoginCredential(
"user",
"123",
undefined,
"username_resource_id",
"password_resource_id",
);
const result = getLoginCredential({
username: "user",
password: "123",
usernameResourceName: "username_resource_id",
passwordResourceName: "password_resource_id",
});

expect(result).to.deep.equal({
username: "user",
Expand All @@ -144,13 +143,12 @@ describe("options-parser-util", () => {
});

it("returns credential for username, passwordFile, and resource names", () => {
const result = getLoginCredential(
"user",
undefined,
const result = getLoginCredential({
username: "user",
passwordFile,
"username_resource_id",
"password_resource_id",
);
usernameResourceName: "username_resource_id",
passwordResourceName: "password_resource_id",
});

expect(result).to.deep.equal({
username: "user",
Expand All @@ -163,24 +161,32 @@ describe("options-parser-util", () => {
});

it("throws error when username and password not provided together", () => {
expect(() => getLoginCredential("user", undefined, undefined)).to.throw(
expect(() => getLoginCredential({ username: "user" })).to.throw(
FirebaseError,
"Username and password for automated tests need to be specified together",
);
});

it("throws error when username but not password resource provided", () => {
it("throws error when password (but not username) resource provided", () => {
expect(() =>
getLoginCredential("user", "123", undefined, undefined, "password_resource_id"),
getLoginCredential({
username: "user",
password: "123",
passwordResourceName: "password_resource_id",
}),
).to.throw(
FirebaseError,
"Username and password resource names for automated tests need to be specified together",
);
});

it("throws error when password but not username resource provided", () => {
it("throws error when password file and password (but not username) resource provided", () => {
expect(() =>
getLoginCredential("user", undefined, passwordFile, undefined, "password_resource_id"),
getLoginCredential({
username: "user",
passwordFile,
passwordResourceName: "password_resource_id",
}),
).to.throw(
FirebaseError,
"Username and password resource names for automated tests need to be specified together",
Expand All @@ -189,13 +195,10 @@ describe("options-parser-util", () => {

it("throws error when resource names provided without username and password", () => {
expect(() =>
getLoginCredential(
undefined,
undefined,
undefined,
"username_resource_id",
"password_resource_id",
),
getLoginCredential({
usernameResourceName: "username_resource_id",
passwordResourceName: "password_resource_id",
}),
).to.throw(FirebaseError, "Must specify username and password");
});
});
Expand Down

0 comments on commit c1f6d9e

Please sign in to comment.