Skip to content

Commit

Permalink
Merge pull request #14 from zazuko/users
Browse files Browse the repository at this point in the history
Adds user
  • Loading branch information
mathewmeconry authored Oct 13, 2019
2 parents e2aba46 + 27d7d6a commit 3dff205
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/interfaces/UsersStatic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export namespace UsersStatic {
export interface UserSmall {
id: number;
nickname?: string;
firstname: string;
lastname: string;
email?: string;
color: string;
phone_fixed?: string;
phone_mobile?: string;
is_superadmin: boolean;
}

export interface UserFull extends UserSmall {
profile_image: string;
}

export enum UsersSearchparameters {
id = "id",
nickname = "nickname",
firstname = "firstname",
lastname = "lastname",
color = "color"
}
}
76 changes: 76 additions & 0 deletions src/resources/Users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import BaseCrud from "./BaseCrud";
import OAuth2 from "../libs/OAuth2";
import { Scopes } from "..";
import { UsersStatic } from "../interfaces/UsersStatic";

export default class Users extends BaseCrud<
UsersStatic.UserSmall,
UsersStatic.UserFull,
UsersStatic.UserSmall,
UsersStatic.UsersSearchparameters,
{},
{}
> {
constructor(bexioAuth: OAuth2) {
super(
bexioAuth,
"/user",
Scopes.GENERAL,
Scopes.GENERAL
);
}

/**
* Not implemented by Bexio yet
*
* @param {number} id
* @param {{}} ressource
* @returns {Promise<UsersStatic.UserFull>}
* @memberof Users
*/
public async overwrite(
id: number,
ressource: {}
): Promise<UsersStatic.UserFull> {
throw new Error("not implemented by Bexio yet");
}

/**
* Not implemented by Bexio yet
*
* @param {number} id
* @param {{}} ressource
* @returns {Promise<UsersStatic.UserFull>}
* @memberof Users
*/
public async edit(
id: number,
ressource: {}
): Promise<UsersStatic.UserFull> {
throw new Error("not implemented by Bexio yet");
}

/**
* Not implemented by Bexio yet
*
* @param {{}} ressource
* @returns {Promise<UsersStatic.UserFull>}
* @memberof Users
*/
public async create(ressource: {}): Promise<
UsersStatic.UserFull
> {
throw new Error("not implemented by Bexio yet");
}

/**
* Not implemented by Bexio yet
*
* @param {number} id
* @returns {Promise<boolean>}
* @memberof Users
*/
public async delete(id: number): Promise<boolean> {
throw new Error("not implemented by Bexio yet");
}
}
142 changes: 142 additions & 0 deletions src/tests/Users.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import Bexio, { Scopes } from "..";
import { expect } from "chai";
import dotenv from "dotenv";
import { UsersStatic } from "../interfaces/UsersStatic";
import Users from "../resources/Users";

dotenv.config();

describe("Users", function() {
// increasing timeout to 60s
this.timeout(60000);

let api: Bexio;
let moduleToTest: Users;
let user: UsersStatic.UserSmall;
const {
BEXIO_CLIENTID,
BEXIO_CLIENTSECRET,
HOSTNAME,
BEXIO_USERNAME,
BEXIO_PASSWORD
} = process.env;

before(async () => {
if (
!BEXIO_CLIENTID ||
!BEXIO_CLIENTSECRET ||
!HOSTNAME ||
!BEXIO_USERNAME ||
!BEXIO_PASSWORD
)
throw new Error("not all necessary variables defined");

api = new Bexio(
BEXIO_CLIENTID,
BEXIO_CLIENTSECRET,
`http://${HOSTNAME}/callback`,
[Scopes.GENERAL, Scopes.GENERAL]
);
await api.fakeLogin(BEXIO_USERNAME, BEXIO_PASSWORD);
});

it("init user", () => {
moduleToTest = new Users(api["bexioAuth"]);
});

it.skip("create new user (not implemented by Bexio yet)", async () => {
user = await moduleToTest.create({
name: "test"
});
});

it("should return a not implemented error on creation", async () => {
user = {
id: 1,
firstname: "Mathias",
lastname: "Scherer",
is_superadmin: false,
color: "#ff00ff"
};
try {
await moduleToTest.create({
name: "test"
});
} catch (err) {
expect(err.message).to.be.eq("not implemented by Bexio yet");
}
});

it("list users", async () => {
const list = await moduleToTest.list({});
expect(list.map(el => el.id)).includes(user.id);
});

it("search users", async () => {
const searchResult = await moduleToTest.search({}, [
{
field:
UsersStatic.UsersSearchparameters.firstname,
value: user.firstname,
criteria: "="
}
]);
expect(searchResult.length).to.be.greaterThan(0);
expect(searchResult[0].id).to.be.eq(user.id);
});

it("show user", async () => {
const showedUser = await moduleToTest.show({}, user.id);
expect(showedUser.lastname).to.be.eq(user.lastname);
});

it.skip("overwrite a user (not implemented by Bexio yet)", async () => {
const overwritten = await moduleToTest.overwrite(
user.id,
{
name: "test"
}
);
expect(overwritten.lastname).to.be.eq("test");
});

it("should return a not implemented error on overwrite", async () => {
try {
await moduleToTest.overwrite(user.id, {
name: "test"
});
} catch (err) {
expect(err.message).to.be.eq("not implemented by Bexio yet");
}
});

it.skip("edit user (not implemented by Bexio yet)", async () => {
const edited = await moduleToTest.edit(user.id, {
name: "test"
});
expect(edited.lastname).to.be.eq("test");
});

it("should return a not implemented error on edit", async () => {
try {
await moduleToTest.edit(user.id, {
name: "test"
});
} catch (err) {
expect(err.message).to.be.eq("not implemented by Bexio yet");
}
});

it.skip("delete user (not implemented by Bexio yet)", async () => {
const result = await moduleToTest.delete(user.id);
expect(result).to.be.true;
});

it("should return a not implemented error on deletion", async () => {
try {
await moduleToTest.delete(user.id);
} catch (err) {
expect(err.message).to.be.eq("not implemented by Bexio yet");
}
});
});

0 comments on commit 3dff205

Please sign in to comment.