Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LX-325 Public data groups #61

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions migrations/2024080611510000-add-user_group_memberships-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

module.exports = {
async up(queryInterface, Sequelize) {
// set access to ReadWrite for all current user_group_memberships
await queryInterface.sequelize.query(
`ALTER TABLE user_group_memberships
ADD access int(11) DEFAULT 3;`
);
await queryInterface.sequelize.query(
`ALTER TABLE user_group_memberships
MODIFY COLUMN access int(11) NOT NULL;`
);
},
async down(queryInterface, Sequelize) {
await queryInterface.sequelize.query(
`ALTER TABLE user_group_memberships
DROP access;`
);
},
};
12 changes: 11 additions & 1 deletion src/queries/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const UserModel = sequelize.define(

username: { type: DataTypes.STRING, allowNull: false },
password: { type: DataTypes.STRING, allowNull: false },
access: DataTypes.INTEGER, // 1 = member, 2 = admin
access: DataTypes.INTEGER,
enabled: DataTypes.INTEGER,
is_super_user: { type: DataTypes.BOOLEAN, allowNull: false },

Expand Down Expand Up @@ -275,6 +275,10 @@ const UserGroupMembershipModel = sequelize.define(
references: { model: UserGroupModel, key: "iduser_groups" },
allowNull: false,
},
access: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
tableName: "user_group_memberships",
Expand Down Expand Up @@ -420,6 +424,12 @@ export enum UserMapAccess {
Readwrite = 3,
}

/* The access values in the UserGroupMembership table */
export enum UserGroupAccess {
Readonly = 1,
Readwrite = 3,
}

/* All the possible values of the iditem_types column in the ItemType table */
export enum ItemTypeId {
Marker = 0,
Expand Down
239 changes: 239 additions & 0 deletions src/queries/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,242 @@ describe("Check and return user", () => {
});
});
});

describe("findAllDataGroupContentForUser", () => {
const testUserId = 123;

afterEach(() => {
sandbox.restore();
});

context(
"There is a user group (1) associated with 1 data group containing 1 marker, and a user group (2) associated with a data group containing 1 polygon",
() => {
const testMarker = {
idmarkers: 1,
name: "Test Marker",
description: "This is a datagroup marker",
data_group_id: 1,
location: {
type: "Feature",
geometry: {
type: "Point",
coordinates: [53.6, 12.1],
},
},
uuid: "abc-001",
};

const testPolygon = {
idpolygons: 1,
name: "Test Polygon",
description: "This is a datagroup polygon",
data_group_id: 2,
vertices: {
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[125.6, 10.1],
[125.7, 10.2],
[125.6, 10.1],
],
],
},
},
center: {
type: "Feature",
geometry: {
type: "Point",
coordinates: [125.63, 10.13],
},
},
length: 100,
area: 1000,
uuid: "fgh-002",
};

const testUserGroup1 = {
iduser_groups: 1,
name: "User Group (1)",
};

const testDataGroup1 = {
iddata_groups: 1,
title: "Data Group (1)",
hex_colour: "#FF0001",
};

const testUserGroup2 = {
iduser_groups: 2,
name: "User Group (2)",
};

const testDataGroup2 = {
iddata_groups: 2,
title: "Data Group (2)",
hex_colour: "#FF0002",
};

beforeEach(() => {
sandbox.replace(
Model.UserGroup,
"findOne",
fake((options) => {
return options.where.iduser_groups === testUserGroup1.iduser_groups
? testUserGroup1
: testUserGroup2;
})
);

sandbox.replace(
Model.DataGroupMembership,
"findAll",
fake((options) => {
return options.where.user_group_id === testUserGroup1.iduser_groups
? [
{
iddata_group_memberships: 1,
data_group_id: testDataGroup1.iddata_groups,
user_group_id: testUserGroup1.iduser_groups,
},
]
: [
{
iddata_group_memberships: 2,
data_group_id: testDataGroup2.iddata_groups,
user_group_id: testUserGroup2.iduser_groups,
},
];
})
);

sandbox.replace(
Model.DataGroup,
"findOne",
fake((options) => {
return options.where.iddata_groups === testDataGroup1.iddata_groups
? testDataGroup1
: testDataGroup2;
})
);

sandbox.replace(
Model.Marker,
"findAll",
fake((options) => {
return options.where.data_group_id === testDataGroup1.iddata_groups
? [testMarker]
: [];
})
);
sandbox.replace(
Model.Polygon,
"findAll",
fake((options) => {
return options.where.data_group_id === testDataGroup2.iddata_groups
? [testPolygon]
: [];
})
);
sandbox.replace(Model.Line, "findAll", fake.resolves([]));
});

it("Returns the datagroups for a usergroup with readwrite access and a public usergroup with readonly access", async () => {
sandbox.replace(
Model.UserGroupMembership,
"findAll",
fake.resolves([
{
iduser_group_memberships: 1,
user_id: -1, // -1 means public
user_group_id: testUserGroup1.iduser_groups,
access: 1, // readonly access
},
{
iduser_group_memberships: 2,
user_id: testUserId,
user_group_id: testUserGroup2.iduser_groups,
access: 3, // readwrite access
},
])
);

const result = await query.findAllDataGroupContentForUser(testUserId);

const expectedContent = [
{
name: testUserGroup1.name,
id: testUserGroup1.iduser_groups,
access: 1,
dataGroups: [
{
...testDataGroup1,
markers: [testMarker],
polygons: [],
lines: [],
},
],
},
{
name: testUserGroup2.name,
id: testUserGroup2.iduser_groups,
access: 3,
dataGroups: [
{
...testDataGroup2,
markers: [],
polygons: [testPolygon],
lines: [],
},
],
},
];

expect(result).to.deep.equal(expectedContent);
});

it("Returns the higher access level if a user has readwrite access to a public usergroup", async () => {
sandbox.replace(
Model.UserGroupMembership,
"findAll",
fake.resolves([
{
iduser_group_memberships: 1,
user_id: -1, // -1 means public
user_group_id: testUserGroup1.iduser_groups,
access: 1, // readonly access
},
{
iduser_group_memberships: 2,
user_id: testUserId,
user_group_id: testUserGroup1.iduser_groups,
access: 3, // readwrite access
},
])
);

const result = await query.findAllDataGroupContentForUser(testUserId);

const expectedContent = [
{
name: testUserGroup1.name,
id: testUserGroup1.iduser_groups,
access: 3,
dataGroups: [
{
...testDataGroup1,
markers: [testMarker],
polygons: [],
lines: [],
},
],
},
];

expect(result).to.deep.equal(expectedContent);
});
}
);
});
Loading
Loading