Skip to content

Commit d06308e

Browse files
committed
Unit testing empty access token
1 parent 289f566 commit d06308e

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

spec/core/Client.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,45 @@ describe("Client.ts", () => {
101101
const response = await client.api("me").get();
102102
assert.equal(response, responseBody);
103103
});
104+
105+
it("Should throw error in case the access token is undefined", async () => {
106+
try {
107+
const options = {
108+
defaultVersion: "v1.0",
109+
debugLogging: true,
110+
authProvider: (done) => {
111+
done(null, getTokenFunction());
112+
},
113+
};
114+
115+
const getTokenFunction = (): string => {
116+
return undefined;
117+
};
118+
const client = Client.init(options);
119+
const res = await client.api("/test").get();
120+
} catch (error) {
121+
assert.isDefined(error.body);
122+
}
123+
});
124+
125+
it("Should throw error in case the access token is empty", async () => {
126+
try {
127+
const options = {
128+
defaultVersion: "v1.0",
129+
debugLogging: true,
130+
authProvider: (done) => {
131+
done(null, getTokenFunction());
132+
},
133+
};
134+
const getTokenFunction = (): string => {
135+
return "";
136+
};
137+
const client = Client.init(options);
138+
const res = await client.api("/test").get();
139+
} catch (error) {
140+
assert.isDefined(error.body);
141+
}
142+
});
104143
});
105144

106145
describe("init", () => {

src/CustomAuthenticationProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @module CustomAuthenticationProvider
1010
*/
1111

12+
import { GraphError } from "./browser";
1213
import { AuthenticationProvider } from "./IAuthenticationProvider";
1314
import { AuthProvider } from "./IAuthProvider";
1415

@@ -48,7 +49,7 @@ export class CustomAuthenticationProvider implements AuthenticationProvider {
4849
resolve(accessToken);
4950
} else {
5051
if (!error) {
51-
reject("Access token cannot be undefined or empty");
52+
error = new GraphError(-1, "Access token cannot be undefined or empty.");
5253
}
5354
reject(error);
5455
}

src/GraphErrorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class GraphErrorHandler {
5959
*/
6060
private static constructErrorFromResponse(error: any, statusCode: number): GraphError {
6161
if (!error.error) {
62-
const graphError = new GraphError(statusCode, "SDK failed to send the request");
62+
const graphError = new GraphError(statusCode, "Microsoft Graph JavaScript Client SDK came across an error while processing the request");
6363
graphError.body = error;
6464
return graphError;
6565
}

0 commit comments

Comments
 (0)