Skip to content

Commit

Permalink
Merge pull request #44 from MOV-AI/feature/FP-1358-support-different-…
Browse files Browse the repository at this point in the history
…authentications

FP-1358 - Support different authentications
  • Loading branch information
RianMartins-Movai authored Mar 8, 2022
2 parents 8ef78a9 + 6989ebd commit aa7ae95
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/Authentication/Authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import jwtDecode from "jwt-decode";

const Authentication = {};

const INTERNAL_AUTHENTICATION = "internal";
const DEFAULT_PROVIDERS = [INTERNAL_AUTHENTICATION];
Authentication.DEFAULT_PROVIDER = INTERNAL_AUTHENTICATION;

Authentication.AuthException = function (message) {
this.message = message;
this.statusText = message;
Expand Down Expand Up @@ -64,15 +68,21 @@ Authentication.deleteTokens = () => {
window.sessionStorage.removeItem("movai.session");
};

Authentication.login = async (username, password, remember = false) => {
Authentication.login = async (
username,
password,
remember = false,
domain = INTERNAL_AUTHENTICATION
) => {
try {
Authentication.deleteTokens();

const url = `/token-auth/`;
const body = {
username: username,
password: password,
remember: remember
remember: remember,
domain
};

const response = await Authentication.request({ url, body });
Expand Down Expand Up @@ -129,6 +139,31 @@ Authentication.checkLogin = async () => {
return await Authentication.refreshTokens();
};

Authentication.getProviders = () => {
const headers = {
"Content-Type": "application/json"
};
const url = `/status/`;
return new Promise(resolve =>
fetch(url, { headers })
.then(response => {
if (!response.ok) {
throw new Error({ error: response.statusText });
}
return response
.json()
.then(resolve)
.catch(error => {
throw new Error({ error });
});
})
.catch(error => {
console.log("Error Fetching Providers: ", error);
resolve({ domains: DEFAULT_PROVIDERS });
})
);
};

Authentication.request = ({ url, body }) => {
return fetch(url, {
method: "POST",
Expand Down
46 changes: 46 additions & 0 deletions src/Authentication/Authentication.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Authentication from "./Authentication";

const providers = ["internal", "domain1"];

global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
domains: providers,
success: true
})
})
);

beforeEach(() => {
fetch.mockClear();
});

test("Get Providers when fetch is successfull", () => {
Authentication.getProviders().then(result => {
expect(result.domains).toEqual(expect.arrayContaining(providers));
});
});

test("Get Providers when fetch is not successfull", () => {
fetch.mockImplementationOnce(() =>
Promise.resolve({
ok: false
})
);
Authentication.getProviders().catch(error =>
expect(result.domains).toEqual(
expect.arrayContaining([Authentication.DEFAULT_PROVIDER])
)
);
});

test("Get Providers when API is down", () => {
fetch.mockImplementationOnce(() => Promise.reject("API is down"));
Authentication.getProviders().catch(error =>
expect(result.domains).toEqual(
expect.arrayContaining([Authentication.DEFAULT_PROVIDER])
)
);
});

0 comments on commit aa7ae95

Please sign in to comment.