Skip to content

Commit

Permalink
Add test for options parser util
Browse files Browse the repository at this point in the history
  • Loading branch information
lfkellogg committed Jan 31, 2024
1 parent 24b90b2 commit bbea92c
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/appdistribution/options-parser-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function parseTestDevice(testDeviceString: string): TestDevice {
break;
default:
throw new FirebaseError(
`Unrecognized key in test devices. Can only contain ${Array.from(allowedKeys).join(",")}`,
`Unrecognized key in test devices. Can only contain ${Array.from(allowedKeys).join(", ")}`,
);
}
}
Expand All @@ -127,12 +127,16 @@ function parseTestDevice(testDeviceString: string): TestDevice {
return { model, version, locale, orientation };
}

/**
* 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,
usernameResourceName?: string,
passwordResourceName?: string,
) {
): LoginCredential | undefined {
if (isPresenceMismatched(usernameResourceName, passwordResourceName)) {
throw new FirebaseError(
"Username and password resource names for automated tests need to be specified together.",
Expand Down
134 changes: 134 additions & 0 deletions src/test/appdistro/options-parser-util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { expect } from "chai";
import { getLoginCredential, getTestDevices } from "../../appdistribution/options-parser-util";
import { FirebaseError } from "../../error";

describe.only("options-parser-util", () => {
describe("getTestDevices", () => {
it("parses a test device", () => {
const optionValue = "model=modelname,version=123,orientation=landscape,locale=en_US";

const result = getTestDevices(optionValue, "");

expect(result).to.deep.equal([{

Check failure on line 12 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `⏎········`

Check failure on line 12 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `⏎········`

Check failure on line 12 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `⏎········`

Check failure on line 12 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `⏎········`
model: "modelname",

Check failure on line 13 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `··`

Check failure on line 13 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `··`

Check failure on line 13 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `··`

Check failure on line 13 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `··`
version: "123",

Check failure on line 14 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `········` with `··········`

Check failure on line 14 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `········` with `··········`

Check failure on line 14 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Replace `········` with `··········`

Check failure on line 14 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `········` with `··········`
orientation: "landscape",

Check failure on line 15 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `··`

Check failure on line 15 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `··`

Check failure on line 15 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `··`

Check failure on line 15 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `··`
locale: "en_US",

Check failure on line 16 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `········` with `··········`

Check failure on line 16 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `········` with `··········`

Check failure on line 16 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Replace `········` with `··········`

Check failure on line 16 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `········` with `··········`
}]);

Check failure on line 17 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `}` with `··},⏎······`

Check failure on line 17 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `}` with `··},⏎······`

Check failure on line 17 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Replace `}` with `··},⏎······`

Check failure on line 17 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `}` with `··},⏎······`
});

it("parses multiple semicolon-separated test devices", () => {
const optionValue =
"model=modelname,version=123,orientation=landscape,locale=en_US;model=modelname2,version=456,orientation=portrait,locale=es";

const result = getTestDevices(optionValue, "");

expect(result).to.deep.equal([
{
model: "modelname",
version: "123",
orientation: "landscape",
locale: "en_US",
},
{
model: "modelname2",
version: "456",
orientation: "portrait",
locale: "es",
}

Check failure on line 38 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 38 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`

Check failure on line 38 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `,`

Check failure on line 38 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`
]);
});

it("parses multiple newline-separated test devices", () => {
const optionValue =
"model=modelname,version=123,orientation=landscape,locale=en_US\nmodel=modelname2,version=456,orientation=portrait,locale=es";

const result = getTestDevices(optionValue, "");

expect(result).to.deep.equal([
{
model: "modelname",
version: "123",
orientation: "landscape",
locale: "en_US",
},
{
model: "modelname2",
version: "456",
orientation: "portrait",
locale: "es",
}

Check failure on line 60 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 60 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`

Check failure on line 60 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `,`

Check failure on line 60 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `,`
]);
});

it("throws an error with correct format when missing a field", () => {
const optionValue = "model=modelname,version=123,locale=en_US";

expect(() => getTestDevices(optionValue, "")).to.throw(
FirebaseError,
"model=<model-id>,version=<os-version-id>,locale=<locale>,orientation=<orientation>",
);
});

it("throws an error with expected fields when field is unexpected", () => {
const optionValue = "model=modelname,version=123,orientation=landscape,locale=en_US,notafield=blah";

Check failure on line 74 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `⏎·······`

Check failure on line 74 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `⏎·······`

Check failure on line 74 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `⏎·······`

Check failure on line 74 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `⏎·······`

expect(() => getTestDevices(optionValue, "")).to.throw(
FirebaseError,
"model, version, orientation, locale",
);
});
});

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

expect(result).to.deep.equal({
username: "user",
password: "123",
fieldHints: undefined,
});
});

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

expect(result).to.be.undefined

Check failure on line 97 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `;`

Check failure on line 97 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

Check failure on line 97 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Insert `;`

Check failure on line 97 in src/test/appdistro/options-parser-util.spec.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`
});

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

expect(result).to.deep.equal({
username: "user",
password: "123",
fieldHints: {
usernameResourceName: "username_resource_id",
passwordResourceName: "password_resource_id",
},
});
});

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

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

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

0 comments on commit bbea92c

Please sign in to comment.