Skip to content

Commit

Permalink
fix(:bug:): ensure fix session.portal url when cred.server contains s…
Browse files Browse the repository at this point in the history
…haring/rest

AFFECTS PACKAGES:
@esri/arcgis-rest-auth
  • Loading branch information
jgravois committed Nov 7, 2018
1 parent 029e648 commit 815de49
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ export class UserSession implements IAuthenticationManager {
*/
static fromCredential(credential: ICredential) {
return new UserSession({
portal: credential.server + `/sharing/rest`,
portal: credential.server.includes("sharing/rest")
? credential.server
: credential.server + `/sharing/rest`,
ssl: credential.ssl,
token: credential.token,
username: credential.userId,
Expand Down
38 changes: 24 additions & 14 deletions packages/arcgis-rest-auth/test/UserSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,21 +961,21 @@ describe("UserSession", () => {
userId: "jsmith"
};

it("should create a credential object from a session", () => {
const session = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "token",
ssl: false,
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "jsmith",
password: "123456"
});
const MOCK_USER_SESSION = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "token",
ssl: false,
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "jsmith",
password: "123456"
});

const creds = session.toCredential();
it("should create a credential object from a session", () => {
const creds = MOCK_USER_SESSION.toCredential();
expect(creds.userId).toEqual("jsmith");
expect(creds.server).toEqual("https://www.arcgis.com/sharing/rest");
expect(creds.ssl).toEqual(false);
Expand All @@ -991,5 +991,15 @@ describe("UserSession", () => {
expect(session.token).toEqual("token");
expect(session.tokenExpires).toEqual(new Date(TOMORROW));
});

it("should create a UserSession from a credential that came from a UserSession", () => {
const creds = MOCK_USER_SESSION.toCredential();
const credSession = UserSession.fromCredential(creds);
expect(credSession.username).toEqual("jsmith");
expect(credSession.portal).toEqual("https://www.arcgis.com/sharing/rest");
expect(credSession.ssl).toEqual(false);
expect(credSession.token).toEqual("token");
expect(credSession.tokenExpires).toEqual(new Date(TOMORROW));
});
});
});

0 comments on commit 815de49

Please sign in to comment.