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

adding tests to github actions #46

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
07b709b
adding tests to github actions
muskansethi1 May 3, 2024
27687a5
update file
muskansethi1 May 3, 2024
8efc2b7
test
muskansethi1 May 3, 2024
574ee21
correction
muskansethi1 May 3, 2024
a4a3e1c
check for dir
muskansethi1 May 3, 2024
2074152
checking directory
muskansethi1 May 3, 2024
f20a648
updated tests config
muskansethi1 May 3, 2024
7fece9e
copy file
muskansethi1 May 3, 2024
cfa2aaa
updated test files and scripts
muskansethi1 May 5, 2024
f990ca4
minor changes
muskansethi1 May 5, 2024
5e27bab
temp test
muskansethi1 May 5, 2024
ffd0abe
updating scripts
muskansethi1 May 5, 2024
3739636
testing in github actions
muskansethi1 May 5, 2024
1b0658e
updating path
muskansethi1 May 5, 2024
30f582b
update installation
muskansethi1 May 5, 2024
023c148
updated test app
muskansethi1 May 5, 2024
9979d7d
updated tests
muskansethi1 May 5, 2024
8aca59a
Merge branch 'main' into github-actions
muskansethi1 Jun 25, 2024
44c9d18
enabling tests in github actions
muskansethi1 Jun 27, 2024
c10cbd6
update yml file
muskansethi1 Jun 27, 2024
5922ac4
update file
muskansethi1 Jun 27, 2024
50123c1
update file
muskansethi1 Jun 27, 2024
f8bae10
temp config
muskansethi1 Jun 27, 2024
b19acbe
test
muskansethi1 Jun 27, 2024
deb8256
test
muskansethi1 Jun 27, 2024
aa336d0
correct work folder
muskansethi1 Jun 27, 2024
3b86635
test
muskansethi1 Jun 27, 2024
b7b4ce2
test
muskansethi1 Jun 27, 2024
7354cf0
update for project path
muskansethi1 Jun 27, 2024
7cf0bf9
replicating calesi setup
muskansethi1 Jun 27, 2024
ce3aef1
Merge branch 'main' into github-actions
muskansethi1 Jun 27, 2024
71de9ac
updated test.yml
muskansethi1 Jun 27, 2024
2326287
updated work folder
muskansethi1 Jun 27, 2024
ffebcaf
temp
muskansethi1 Jun 27, 2024
961993b
test
muskansethi1 Jun 27, 2024
9e5fdcb
test
muskansethi1 Jun 27, 2024
b2e65e1
test
muskansethi1 Jun 27, 2024
4a34a56
replicating calesi setup
muskansethi1 Jun 27, 2024
83ec63a
recursive copy
muskansethi1 Jun 27, 2024
c5310ae
updating attachments location
muskansethi1 Jun 27, 2024
74b4c62
Merge branch 'main' into github-actions
muskansethi1 Jul 16, 2024
d0ed547
bug fixes
muskansethi1 Jul 16, 2024
a3b4028
add sqlite for actions
muskansethi1 Jul 16, 2024
07b4b99
update
muskansethi1 Jul 16, 2024
d569c68
update
muskansethi1 Jul 17, 2024
2f1d6d5
Merge branch 'main' into github-actions
muskansethi1 Jul 29, 2024
3b7f557
Merge branch 'main' into github-actions
muskansethi1 Jul 30, 2024
d5a0499
Merge branch 'main' into github-actions
muskansethi1 Aug 8, 2024
01fe08c
Merge branch 'main' into github-actions
muskansethi1 Aug 12, 2024
386924f
temp
muskansethi1 Aug 12, 2024
5b66654
update
muskansethi1 Aug 12, 2024
211ff92
update
muskansethi1 Aug 12, 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
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened, auto_merge_enabled]

# Allow parallel jobs on `main`, so that each commit is tested. For PRs, run only the latest commit.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
name: Node.js ${{ matrix.node }}

strategy:
fail-fast: true
matrix:
node: [20, 18]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- run: npm i
#replicating calesi folder structure
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this necessary? shouldn't it just be npm i && npm t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am cloning incidents-app here, hence replicated the calesi setup so that the tests can run both here and in local in the same setup

- run: cd .. && mkdir calesi
- run: cd ../calesi && mkdir plugins && cd plugins && mkdir attachments
- run: cp -r ./* ../calesi/plugins/attachments
- run: cd ../calesi && git clone https://github.com/cap-js/incidents-app.git && cd incidents-app && npm install --save ../plugins/attachments/ && cp -r xmpls/attachments.cds ./db && npm i
- run: cd ../calesi/plugins/attachments && npm run test
env:
FORCE_COLOR: true
- run: rm calesi
10 changes: 5 additions & 5 deletions lib/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ module.exports = class AttachmentsService extends cds.Service {
}

async update(Attachments, key, data) {
DEBUG?.("Updating attachment for", Attachments.name, key)
return await UPDATE(Attachments, key).with(data)
DEBUG?.("Updating attachment for", Attachments.name, key);
return await UPDATE(Attachments, key).with(data);
}

async getStatus(Attachments, key) {
const result = await SELECT.from(Attachments, key).columns('status')
return result.status
const result = await SELECT.from(Attachments, key).columns('status');
return result?.status;
}

async deleteInfectedAttachment(Attachments, key) {
return await UPDATE(Attachments, key).with({ content: null})
return await UPDATE(Attachments, key).with({ content: null});
}
};
1 change: 1 addition & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ cds.once("served", async function registerPluginHandlers() {
if(req._path?.endsWith("content")) {
const status = await AttachmentsSrv.getStatus(req.target, req.params.at(-1));
const scanEnabled = cds.env.requires?.attachments?.scan ?? true
//TODO: add error in case content doesn't exist
if(scanEnabled && status !== 'Clean') {
req.reject(403, 'Unable to download the attachment as scan status is not clean.');
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"srv"
],
"scripts": {
"lint": "npx eslint ."
"lint": "npx eslint .",
"test": "npx jest attachments.test.js"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.400.0",
Expand Down
61 changes: 13 additions & 48 deletions tests/integration/attachments.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cds = require("@sap/cds");
const incidentsApp = require("path").resolve(__dirname, "./../../xmpl");
const path = require("path");
const incidentsApp = path.resolve(__dirname, "./../../../../incidents-app");
const { expect, axios, GET, POST, DELETE } = cds.test(incidentsApp);
const { RequestSend } = require("../utils/api");
const { createReadStream } = cds.utils.fs;
Expand All @@ -8,54 +9,21 @@
axios.defaults.auth = { username: "alice" };
jest.setTimeout(5 * 60 * 1000);

const utils = new RequestSend(POST);
let utils = null;
let sampleDocID = null;
let incidentID = null;

describe("Tests for mock data in xmpl attachments - in-memory db", () => {
beforeAll(() => {
sampleDocID = null;
incidentID = "3b23bb4b-4ac7-4a24-ac02-aa10cabd842c";
});

//Reading the attachment list and checking for content
it("Reading attachments list", async () => {
//read attachments list for Incident - Inverter not functional
try {
const response = await GET(
`odata/v4/processor/Incidents(ID=${incidentID},IsActiveEntity=true)/attachments`
);
//the mock data has two attachments in this incident
expect(response.status).to.equal(200);
expect(response.data.value.length).to.equal(2);
sampleDocID = response.data.value[0].ID;
//to make sure content is not read
expect(response.data.value[0].content).to.be.undefined;
} catch (err) {
expect(err).to.be.undefined;
}
});

//Reading the uploaded attachment content and that it exists
it("Reading the uploaded attachment document", async () => {
//checking the uploaded attachment document

try {
const response = await GET(
`odata/v4/processor/Incidents(ID=${incidentID},IsActiveEntity=true)/attachments(up__ID=${incidentID},ID=${sampleDocID},IsActiveEntity=true)/content`
);
expect(response.status).to.equal(200);
expect(response.data).to.not.be.undefined;
} catch (err) {
expect(err).to.be.undefined;
}
});
});
let db = null;

Check warning on line 15 in tests/integration/attachments.test.js

View workflow job for this annotation

GitHub Actions / lint

'db' is assigned a value but never used
let attachmentsService = null;

Check warning on line 16 in tests/integration/attachments.test.js

View workflow job for this annotation

GitHub Actions / lint

'attachmentsService' is assigned a value but never used

describe("Tests for uploading/deleting attachments through API calls - in-memory db", () => {
beforeAll(async () => {
cds.env.requires.db.kind = "sql"
cds.env.requires.attachments.kind = "db";
db = await cds.connect.to("sql:my.db");
attachmentsService = await cds.connect.to("attachments");
sampleDocID = null;
incidentID = "3ccf474c-3881-44b7-99fb-59a2a4668418";
utils = new RequestSend(POST);
});

//Draft mode uploading attachment
Expand All @@ -72,7 +40,7 @@
createdAt: new Date(
Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000
),
createdBy: "alice",
createdBy: "alice"
}
);

Expand All @@ -96,13 +64,10 @@
);
//the data should have two attachments
expect(response.status).to.equal(200);
expect(response.data.value.length).to.equal(2);
expect(response.data.value.length).to.equal(1);
//to make sure content is not read
expect(response.data.value[0].content).to.be.undefined;
sampleDocID =
response.data.value[0].filename == "sample.pdf"
? response.data.value[0].ID
: response.data.value[1].ID;
sampleDocID = response.data.value[0].ID
} catch (err) {
expect(err).to.be.undefined;
}
Expand Down Expand Up @@ -151,7 +116,7 @@

//content should not be there
try {
const response = await GET(

Check warning on line 119 in tests/integration/attachments.test.js

View workflow job for this annotation

GitHub Actions / lint

'response' is assigned a value but never used
`odata/v4/processor/Incidents(ID=${incidentID},IsActiveEntity=true)/attachments(up__ID=${incidentID},ID=${sampleDocID},IsActiveEntity=true)/content`
);
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/content/attachments.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using { sap.capire.incidents as my } from './schema';
using { Attachments } from '@cap-js/attachments';

extend my.Incidents with {
attachments: Composition of many Attachments;
}
Loading