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

Jobs tests #1286

Merged
merged 46 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fbc463b
first tests
sofyalaski May 8, 2024
0592631
feat: reset dataset before every api test (#1131)
Junjiequan Apr 2, 2024
bf8fc0f
Fixes after jobs testing:
sofyalaski May 23, 2024
0d586fc
Fixes after jobs testing:
sofyalaski May 23, 2024
54ee6a6
Fixes after testing:
sofyalaski May 23, 2024
f871c62
dataset(s)Validation redundancy after a merged PR
sofyalaski May 23, 2024
8b8f470
test Jobs authorization wip
sofyalaski May 23, 2024
ea76968
fix filtering syntax
sofyalaski Jun 11, 2024
c7fc938
fixes after Job testing:
sofyalaski Jun 11, 2024
39f12b4
minor changes
sofyalaski Jun 11, 2024
2716245
fix findOne filtering syntax
sofyalaski Jun 11, 2024
186ac28
add Jobs tests
sofyalaski Jun 11, 2024
5edb913
remove redundancy
sofyalaski Jun 13, 2024
c6fd75b
fix numbering
sofyalaski Jun 13, 2024
75f41d6
extend UPDATE_JOB_GROUP user's right to match docs
sofyalaski Jun 14, 2024
83a2743
remove unneccessary arguments
sofyalaski Jun 14, 2024
7b80f14
make function return value of findOne an instance of JobClass
sofyalaski Jun 14, 2024
52a791b
add statusUpdate tests
sofyalaski Jun 14, 2024
af31a2a
add jobParams to the job instance
sofyalaski Jun 17, 2024
c7dd474
Patch tests
sofyalaski Jun 17, 2024
6630770
merge changes from release-jobs
sofyalaski Jun 20, 2024
ea61e32
Fixes after tests:
sofyalaski Jun 25, 2024
4e7c618
add deleteJobGroups from env to configuration
sofyalaski Jun 25, 2024
56a66ad
add username for anonymous user when creating/updating status of the job
sofyalaski Jun 25, 2024
fc8c3ae
statusUpdate instead of update
sofyalaski Jun 25, 2024
47cef8e
Fixes after testing:
sofyalaski Jun 25, 2024
2718699
Finalized tests for authorization
sofyalaski Jun 25, 2024
29287bf
add delete_job_groups definition
sofyalaski Jun 25, 2024
8810393
fix unit tests after test jobconfig.json changes
sofyalaski Jun 26, 2024
31d148e
empty collections before testing
sofyalaski Jul 1, 2024
e274398
changes requested in PR
sofyalaski Jul 1, 2024
6f50600
eremove version duplicate
sofyalaski Jul 2, 2024
eb1962f
Merge remote-tracking branch 'upstream/release-jobs' into jobs-tests
sofyalaski Jul 11, 2024
070e964
change getJobMatchingConfiguration function to explicitly use job typ…
sofyalaski Jul 14, 2024
9fa1b8e
run full testing workflow on pushing to release branches
sofyalaski Jul 14, 2024
759ddfb
add env variable for jobs config path for github workflow
sofyalaski Jul 14, 2024
6667320
Fix lint errors
sbliven Jul 23, 2024
7655856
Merge branch 'release-jobs' into jobs-tests
sbliven Jul 23, 2024
420f1df
Fix lint following merge
sbliven Jul 23, 2024
46ade45
Update test/config/pretest.js to use "dotenv"
despadam Jul 25, 2024
8a07ea2
Fix pretest.js after merging suggestions
despadam Jul 25, 2024
5210a82
Fix pretest.js
despadam Jul 25, 2024
9df58ba
Fix jobconfig no-explicit-any issues
sbliven Jul 26, 2024
7c9c8ae
Fix jobconfig no-explicit-any issue (missed logaction)
sbliven Jul 26, 2024
4b290cb
changes in tests:
sofyalaski Jul 29, 2024
819fdd7
address comments in the PR:
sofyalaski Jul 29, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ jobs:
CREATE_DATASET_WITH_PID_GROUPS: "group2"
CREATE_DATASET_PRIVILEGED_GROUPS: "datasetIngestor,group3"
ACCESS_GROUPS_STATIC_VALUES: "ess"
CREATE_JOB_GROUPS: group1,group2
UPDATE_JOB_GROUPS: group1
DELETE_JOB_GROUPS: "archivemanager"
PROPOSAL_GROUPS: "proposalingestor"
SAMPLE_PRIVILEGED_GROUPS: "sampleingestor"
SAMPLE_GROUPS: "group1"
Expand Down
59 changes: 41 additions & 18 deletions src/casl/casl-ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,36 +818,51 @@ export class CaslAbilityFactory {
["configuration.create.auth" as string]: CreateJobAuth.DatasetPublic,
datasetsValidation: true,
});
can(AuthOp.JobStatusUpdateConfiguration, JobClass, {
["configuration.statusUpdate.auth" as string]: StatusUpdateJobAuth.All,
ownerGroup: undefined
});
} else {
/**
* authenticated users
*/

// check if this user is part of the admin group
if (
user.currentGroups.some((g) => configuration().adminGroups.includes(g))
) {
/**
* authenticated users belonging to any of the group listed in ADMIN_GROUPS
*/

// -------------------------------------
// endpoint authorization
can(AuthOp.JobRead, JobClass);
can(AuthOp.JobCreate, JobClass);
can(AuthOp.JobStatusUpdate, JobClass);
cannot(AuthOp.JobDelete, JobClass);

// -------------------------------------
// data instance authorization
can(AuthOp.JobReadAny, JobClass);
can(AuthOp.JobCreateAny, JobClass);
can(AuthOp.JobStatusUpdateAny, JobClass);
} else if (
user.currentGroups.some((g) => configuration().deleteJobGroups.includes(g))
){
/**
* authenticated users belonging to any of the group listed in DELETE_JOB_GROUPS
*/
// -------------------------------------
// endpoint authorization
can(AuthOp.JobDelete, JobClass);

// -------------------------------------
// data instance authorization
can(AuthOp.JobDeleteAny, JobClass);
} else {
const jobUserAuthorizationValues = [
...user.currentGroups.map((g) => "@" + g),
user.username,
];

if (
user.currentGroups.some((g) =>
configuration().createJobGroups.includes(g),
Expand Down Expand Up @@ -880,7 +895,7 @@ export class CaslAbilityFactory {
];
const jobCreateInstanceAuthorizationValues = [
...Object.values(CreateJobAuth).filter(
(v) => ~String(v).includes("#dataset"),
(v) => !String(v).includes("#dataset"),
),
...jobUserAuthorizationValues,
];
Expand All @@ -889,13 +904,13 @@ export class CaslAbilityFactory {
String(v).includes("#dataset"),
),
];

// -------------------------------------
// endpoint authorization
can(AuthOp.JobRead, JobClass);

if (
configuration().jobConfiguration.some(
(j) => j.create.auth! in jobCreateEndPointAuthorizationValues,
(j) => j.create.auth && jobCreateEndPointAuthorizationValues.includes(j.create.auth as string),
)
) {
can(AuthOp.JobCreate, JobClass);
Expand All @@ -907,6 +922,8 @@ export class CaslAbilityFactory {
ownerGroup: { $in: user.currentGroups },
ownerUser: user.username,
});


can(AuthOp.JobCreateConfiguration, JobClass, {
["configuration.create.auth" as string]: {
$in: jobCreateInstanceAuthorizationValues,
Expand All @@ -916,9 +933,19 @@ export class CaslAbilityFactory {
["configuration.create.auth" as string]: {
$in: jobCreateDatasetAuthorizationValues,
},
datasetValidation: true,
datasetsValidation: true,
});
}
const jobUpdateEndPointAuthorizationValues = [
...Object.values(StatusUpdateJobAuth),
...jobUserAuthorizationValues,
];
const jobUpdateInstanceAuthorizationValues = [
...Object.values(StatusUpdateJobAuth).filter(
(v) => !String(v).includes("#job"),
),
...jobUserAuthorizationValues,
];

if (
user.currentGroups.some((g) =>
Expand All @@ -931,29 +958,24 @@ export class CaslAbilityFactory {

// -------------------------------------
// data instance authorization
can(AuthOp.JobStatusUpdateConfiguration, JobClass, {
["configuration.statusUpdate.auth" as string]: {
$in: jobUpdateInstanceAuthorizationValues,
},
});
can(AuthOp.JobStatusUpdateOwner, JobClass, {
ownerUser: user.username,
});
can(AuthOp.JobStatusUpdateOwner, JobClass, {
ownerGroup: { $in: user.currentGroups },
});
} else {
const jobUpdateEndPointAuthorizationValues = [
...Object.values(StatusUpdateJobAuth),
...jobUserAuthorizationValues,
];
const jobUpdateInstanceAuthorizationValues = [
...Object.values(StatusUpdateJobAuth).filter(
(v) => ~String(v).includes("#job"),
),
...jobUserAuthorizationValues,
];

// -------------------------------------
// endpoint authorization
if (
configuration().jobConfiguration.some(
(j) => j.statusUpdate.auth! in jobUpdateEndPointAuthorizationValues,
(j) => j.statusUpdate.auth && jobUpdateEndPointAuthorizationValues.includes(j.statusUpdate.auth as string),
)
) {
can(AuthOp.JobStatusUpdate, JobClass);
Expand All @@ -975,6 +997,7 @@ export class CaslAbilityFactory {
ownerGroup: { $in: user.currentGroups },
});
}
cannot(AuthOp.JobDelete, JobClass);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const configuration = () => {

const createJobGroups = process.env.CREATE_JOB_GROUPS || ("" as string);
const statusUpdateJobGroups = process.env.UPDATE_JOB_GROUPS || ("" as string);
const deleteJobGroups = process.env.DELETE_JOB_GROUPS || ("" as string);

const proposalGroups = process.env.PROPOSAL_GROUPS || ("" as string);
const sampleGroups = process.env.SAMPLE_GROUPS || ("#all" as string);
Expand Down Expand Up @@ -108,6 +109,7 @@ const configuration = () => {
datasetCreationValidationRegex: datasetCreationValidationRegex,
createJobGroups: createJobGroups,
statusUpdateJobGroups: statusUpdateJobGroups,
deleteJobGroups:deleteJobGroups,
logoutURL: process.env.LOGOUT_URL ?? "", // Example: http://localhost:3000/
accessGroupsGraphQlConfig: {
enabled: boolean(process.env?.ACCESS_GROUPS_GRAPHQL_ENABLED || false),
Expand Down
4 changes: 2 additions & 2 deletions src/jobs/config/jobconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe("Job configuration", () => {
const path = "test/config/jobconfig.json";
const config = await loadJobConfig(path);
expect(config).toBeDefined();
expect(config.length).toBe(1);
expect(config[0].jobType).toBe("archive");
expect(config.length).toBe(7);
expect(config[0].jobType).toBe("all_access");
expect(config[0].create).toBeDefined();
const create = config[0].create;
expect(create.actions.length).toBe(1);
Expand Down
9 changes: 7 additions & 2 deletions src/jobs/jobs.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { EventEmitter2 } from "@nestjs/event-emitter";
import { Test, TestingModule } from "@nestjs/testing";
import { CaslAbilityFactory } from "src/casl/casl-ability.factory";
import { CaslModule } from "src/casl/casl.module";
//import { CaslAbilityFactory } from "src/casl/casl-ability.factory";
import { DatasetsService } from "src/datasets/datasets.service";
import { OrigDatablocksService } from "src/origdatablocks/origdatablocks.service";
import { JobsController } from "./jobs.controller";
import { JobsService } from "./jobs.service";
import { UsersService } from "src/users/users.service";

class JobsServiceMock {}
class DatasetsServiceMock {}
class OrigDatablocksServiceMock {}
class UsersServiceMock {}

describe("JobsController", () => {
let controller: JobsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [JobsController],
imports:[CaslModule],
providers: [
CaslAbilityFactory,
//CaslAbilityFactory,
{ provide: JobsService, useClass: JobsServiceMock },
{ provide: DatasetsService, useClass: DatasetsServiceMock },
{ provide: OrigDatablocksService, useClass: OrigDatablocksServiceMock },
{ provide: UsersService, useClass: UsersServiceMock},
{ provide: EventEmitter2, useClass: EventEmitter2 },
],
}).compile();
Expand Down
Loading