Skip to content

Commit

Permalink
refactor isAuthenticated SDK api to erase session state when authoriz…
Browse files Browse the repository at this point in the history
…ation failed on 'code-push release' call.
  • Loading branch information
Max P committed Sep 29, 2016
1 parent 4b6df54 commit e410244
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
13 changes: 3 additions & 10 deletions cli/script/command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,16 +1172,9 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {

return getPackageFilePromise
.then((file: IPackageFile): Promise<void> => {
return sdk.isAuthenticatedWithMessage()
.then((result: any): Promise<void> => {
if (result.authenticated){
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress);
} else {
throw <CodePushError>{
statusCode: result.status,
message: result.errorMessage
};
}
return sdk.isAuthenticated(true)
.then((isAuth: boolean): Promise<void> => {
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress);
})
.then((): void => {
log("Successfully released an update containing the \"" + command.package + "\" " + (isSingleFilePackage ? "file" : "directory") + " to the \"" + command.deploymentName + "\" deployment of the \"" + command.appName + "\" app.");
Expand Down
17 changes: 7 additions & 10 deletions sdk/script/management-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AccountManager {
return this._accessKey;
}

public isAuthenticatedWithMessage(): Promise<any> {
public isAuthenticated(throwIfUnauthorized?: boolean): Promise<boolean> {
return Promise<any>((resolve, reject, notify) => {
var request: superagent.Request<any> = superagent.get(this._serverUrl + urlEncode `/authenticated`);
if (this._proxy) (<any>request).proxy(this._proxy);
Expand All @@ -90,18 +90,15 @@ class AccountManager {
}

var authenticated: boolean = status === 200;
var errorMessage: string = authenticated ? null : this.getErrorMessage(err, res);

resolve({authenticated, status, errorMessage});
});
});
}
if (!authenticated && throwIfUnauthorized){
reject(this.getCodePushError(err, res));
return;
}

public isAuthenticated(): Promise<boolean> {
return this.isAuthenticatedWithMessage()
.then((res: any) => {
return res.authenticated;
resolve(authenticated);
});
});
}

public addAccessKey(friendlyName: string, ttl?: number): Promise<AccessKey> {
Expand Down
14 changes: 14 additions & 0 deletions sdk/test/management-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ describe("Management SDK", () => {
});
});

it("isAuthenticated handles unsuccessful auth with promise rejection", (done: MochaDone) => {
mockReturn("Unauthorized", 401, {});

// use optional parameter to ask for rejection of the promise if not authenticated
manager.isAuthenticated(true)
.done((authenticated: boolean) => {
assert.fail("isAuthenticated should have rejected the promise");
done();
}, (err) => {
assert.equal(err.message, "Unauthorized", "Error message should be 'Unauthorized'");
done();
});
});

it("isAuthenticated handles unexpected status codes", (done: MochaDone) => {
mockReturn("Not Found", 404, {});
manager.isAuthenticated()
Expand Down

0 comments on commit e410244

Please sign in to comment.