Skip to content

Commit

Permalink
auth: Add getUserInfo
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
otaviojacobi committed Aug 10, 2023
1 parent a3dabec commit ed7cbad
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import * as errors from 'balena-errors';
import memoizee from 'memoizee';
import type { InjectedDependenciesParam, InjectedOptionsParam } from '.';
import { WhoamiResult } from './types/auth';
import { UserInfo, WhoamiResult } from './types/auth';

const getAuth = function (
deps: InjectedDependenciesParam,
Expand Down Expand Up @@ -279,6 +279,21 @@ const getAuth = function (
});
}

async function getUserInfo(): Promise<UserInfo> {
const actor = await getActorDetails();

if (actor.actorType !== 'user') {
throw new Error(
'The authentication credentials in use are not of a user',
);
}
return {
id: actor.actorTypeId,
email: actor.email,
username: actor.username,
};
}

/**
* @summary Get current logged in user's id
* @name getUserId
Expand Down Expand Up @@ -501,6 +516,7 @@ const getAuth = function (
getUserId,
getUserActorId,
getEmail,
getUserInfo,
logout,
register,
verifyEmail,
Expand Down
6 changes: 6 additions & 0 deletions src/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ export type WhoamiResult =
| UserKeyWhoAmIResponse
| ApplicationKeyWhoAmIResponse
| DeviceKeyWhoAmIResponse;

export interface UserInfo {
id: number;
username: string;
email: string | null;
}
60 changes: 60 additions & 0 deletions tests/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaNotLoggedIn',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -215,6 +225,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaNotLoggedIn',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -265,6 +285,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const userInfo = await balena.auth.getUserInfo();
expect(userInfo.email).to.equal(credentials.email);
expect(userInfo.username).to.equal(credentials.username);
expect(userInfo.id).to.be.a('number');
expect(userInfo.id).to.be.greaterThan(0);
});
});

describe('balena.auth.getUserId()', () => {
it('should eventually be a user id', async () => {
const userId = await balena.auth.getUserId();
Expand Down Expand Up @@ -321,6 +351,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'message',
'The authentication credentials in use are not of a user',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async () => {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -389,6 +429,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'message',
'The authentication credentials in use are not of a user',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async () => {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -453,6 +503,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const userInfo = await balena.auth.getUserInfo();
expect(userInfo.email).to.equal(credentials.email);
expect(userInfo.username).to.equal(credentials.username);
expect(userInfo.id).to.be.a('number');
expect(userInfo.id).to.be.greaterThan(0);
});
});

describe('balena.auth.getUserId()', () => {
it('should eventually be a user id', async () => {
const userId = await balena.auth.getUserId();
Expand Down

0 comments on commit ed7cbad

Please sign in to comment.