Skip to content

Commit bbea92c

Browse files
committed
Add test for options parser util
1 parent 24b90b2 commit bbea92c

2 files changed

Lines changed: 140 additions & 2 deletions

File tree

src/appdistribution/options-parser-util.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function parseTestDevice(testDeviceString: string): TestDevice {
114114
break;
115115
default:
116116
throw new FirebaseError(
117-
`Unrecognized key in test devices. Can only contain ${Array.from(allowedKeys).join(",")}`,
117+
`Unrecognized key in test devices. Can only contain ${Array.from(allowedKeys).join(", ")}`,
118118
);
119119
}
120120
}
@@ -127,12 +127,16 @@ function parseTestDevice(testDeviceString: string): TestDevice {
127127
return { model, version, locale, orientation };
128128
}
129129

130+
/**
131+
* Takes option values for username and password related options and returns a LoginCredential
132+
* object that can be passed to the API.
133+
*/
130134
export function getLoginCredential(
131135
username?: string,
132136
password?: string,
133137
usernameResourceName?: string,
134138
passwordResourceName?: string,
135-
) {
139+
): LoginCredential | undefined {
136140
if (isPresenceMismatched(usernameResourceName, passwordResourceName)) {
137141
throw new FirebaseError(
138142
"Username and password resource names for automated tests need to be specified together.",
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { expect } from "chai";
2+
import { getLoginCredential, getTestDevices } from "../../appdistribution/options-parser-util";
3+
import { FirebaseError } from "../../error";
4+
5+
describe.only("options-parser-util", () => {
6+
describe("getTestDevices", () => {
7+
it("parses a test device", () => {
8+
const optionValue = "model=modelname,version=123,orientation=landscape,locale=en_US";
9+
10+
const result = getTestDevices(optionValue, "");
11+
12+
expect(result).to.deep.equal([{
13+
model: "modelname",
14+
version: "123",
15+
orientation: "landscape",
16+
locale: "en_US",
17+
}]);
18+
});
19+
20+
it("parses multiple semicolon-separated test devices", () => {
21+
const optionValue =
22+
"model=modelname,version=123,orientation=landscape,locale=en_US;model=modelname2,version=456,orientation=portrait,locale=es";
23+
24+
const result = getTestDevices(optionValue, "");
25+
26+
expect(result).to.deep.equal([
27+
{
28+
model: "modelname",
29+
version: "123",
30+
orientation: "landscape",
31+
locale: "en_US",
32+
},
33+
{
34+
model: "modelname2",
35+
version: "456",
36+
orientation: "portrait",
37+
locale: "es",
38+
}
39+
]);
40+
});
41+
42+
it("parses multiple newline-separated test devices", () => {
43+
const optionValue =
44+
"model=modelname,version=123,orientation=landscape,locale=en_US\nmodel=modelname2,version=456,orientation=portrait,locale=es";
45+
46+
const result = getTestDevices(optionValue, "");
47+
48+
expect(result).to.deep.equal([
49+
{
50+
model: "modelname",
51+
version: "123",
52+
orientation: "landscape",
53+
locale: "en_US",
54+
},
55+
{
56+
model: "modelname2",
57+
version: "456",
58+
orientation: "portrait",
59+
locale: "es",
60+
}
61+
]);
62+
});
63+
64+
it("throws an error with correct format when missing a field", () => {
65+
const optionValue = "model=modelname,version=123,locale=en_US";
66+
67+
expect(() => getTestDevices(optionValue, "")).to.throw(
68+
FirebaseError,
69+
"model=<model-id>,version=<os-version-id>,locale=<locale>,orientation=<orientation>",
70+
);
71+
});
72+
73+
it("throws an error with expected fields when field is unexpected", () => {
74+
const optionValue = "model=modelname,version=123,orientation=landscape,locale=en_US,notafield=blah";
75+
76+
expect(() => getTestDevices(optionValue, "")).to.throw(
77+
FirebaseError,
78+
"model, version, orientation, locale",
79+
);
80+
});
81+
});
82+
83+
describe("getLoginCredential", () => {
84+
it("returns credential for username and password", () => {
85+
const result = getLoginCredential("user", "123");
86+
87+
expect(result).to.deep.equal({
88+
username: "user",
89+
password: "123",
90+
fieldHints: undefined,
91+
});
92+
});
93+
94+
it("returns undefined when no options provided", () => {
95+
const result = getLoginCredential();
96+
97+
expect(result).to.be.undefined
98+
});
99+
100+
it("returns credential for username, password, and resource names", () => {
101+
const result = getLoginCredential("user", "123", "username_resource_id", "password_resource_id");
102+
103+
expect(result).to.deep.equal({
104+
username: "user",
105+
password: "123",
106+
fieldHints: {
107+
usernameResourceName: "username_resource_id",
108+
passwordResourceName: "password_resource_id",
109+
},
110+
});
111+
});
112+
113+
it("throws error when username and password not provided together", () => {
114+
expect(() => getLoginCredential("user", undefined)).to.throw(
115+
FirebaseError,
116+
"Username and password for automated tests need to be specified together"
117+
);
118+
});
119+
120+
it("throws error when username but not password resources not provided together", () => {
121+
expect(() => getLoginCredential("user", "123", undefined, "password_resource_id")).to.throw(
122+
FirebaseError,
123+
"Username and password resource names for automated tests need to be specified together"
124+
);
125+
});
126+
127+
it("throws error when resource names provided without username and password", () => {
128+
expect(() => getLoginCredential(undefined, undefined, "username_resource_id", "password_resource_id")).to.throw(
129+
FirebaseError,
130+
"Must specify username and password"
131+
);
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)