Skip to content

Commit

Permalink
Merge pull request #137 from Kpoke/feat/grower_images
Browse files Browse the repository at this point in the history
feat/grower images
  • Loading branch information
Kpoke authored Jan 31, 2023
2 parents b14c452 + 2f6f1d0 commit a61389e
Show file tree
Hide file tree
Showing 22 changed files with 1,848 additions and 3,829 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ database.json

# nyc test coverage
.nyc_output

tmp
65 changes: 65 additions & 0 deletions __tests__/integration/grower_account/grower_account.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require('supertest');
const chai = require('chai');
const { v4: uuid } = require('uuid');
const sinon = require('sinon');

const { expect } = chai;
chai.use(require('chai-like'));
Expand All @@ -9,6 +10,7 @@ const app = require('../../../server/app');
const grower_account1 = require('../../mock/grower_account1.json');
const grower_account2 = require('../../mock/grower_account2.json');
const { knex } = require('../../utils');
const s3 = require('../../../server/infra/S3/s3');

describe('/grower_account', () => {
const growerAccountUpdates = {
Expand All @@ -25,6 +27,7 @@ describe('/grower_account', () => {

after(async () => {
await knex('grower_account_org').del();
await knex('grower_account_image').del();
await knex('grower_account').del();
await knex('planter').del();
});
Expand All @@ -42,6 +45,7 @@ describe('/grower_account', () => {
});
expect(typeof res.body.reference_id).eql('number');
expect(res.body.organizations.length).to.eql(0);
expect(res.body.images.length).to.eql(0);

const res2 = await request(app)
.post(`/grower_accounts`)
Expand All @@ -54,6 +58,7 @@ describe('/grower_account', () => {
});
expect(typeof res2.body.reference_id).eql('number');
expect(res2.body.organizations.length).to.eql(0);
expect(res2.body.images.length).to.eql(0);
});

it('should not error out if duplicate wallet is sent', async () => {
Expand All @@ -67,6 +72,7 @@ describe('/grower_account', () => {
...grower_account1,
});
expect(res.body.organizations.length).to.eql(0);
expect(res.body.images.length).to.eql(0);
});
});

Expand Down Expand Up @@ -157,4 +163,63 @@ describe('/grower_account', () => {
});
});
});

describe('GROWER ACCOUNT IMAGES', () => {
let imageId;
let growerAccountId;
it('POST /grower_accounts/image', async () => {
const growerAccount = await knex('grower_account')
.select('id')
.where({ status: 'active' });
growerAccountId = growerAccount[0].id;
const s3Stub = sinon.stub(s3, 'upload').returns({
promise: () => {
return { Location: 'https://location.com' };
},
});

const res = await request(app)
.post(`/grower_accounts/image`)
.set('Accept', 'multipart/form-data')
.field('grower_account_id', growerAccountId)
.attach('image', `${__dirname}/../../mock/test.jpeg`)
.expect(201);

const addedImage = res.body;

imageId = addedImage.id;
expect(addedImage.image_url).eql('https://location.com');
expect(addedImage.grower_account_id).eql(growerAccountId);
expect(addedImage.active).eql(true);

const result = await request(app)
.get(`/grower_accounts/${growerAccountId}`)
.expect(200);
expect(result.body.images.length).eql(1);
expect(result.body.images[0].id).eql(imageId);
expect(result.body.images[0].image_url).eql('https://location.com');

s3Stub.restore();
});

it('PATCH /grower_accounts/image/:image_id', async () => {
const res = await request(app)
.patch(`/grower_accounts/image/${imageId}`)
.send({ active: false })
.expect(200);

expect(res.body.active).eql(false);

const result = await request(app)
.get(`/grower_accounts/${growerAccountId}`)
.expect(200);
expect(result.body.images.length).eql(0);

const growerAccountImage = await knex('grower_account_image')
.select('id')
.where({ active: false });

expect(growerAccountImage.length).eql(1);
});
});
});
Binary file added __tests__/mock/test.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions database/migrations/20230130192407-grower-account-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20230130192407-grower-account-table-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20230130192407-grower-account-table-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE grower_account_image;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE grower_account_image
(
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
grower_account_id uuid NOT NULL REFERENCES grower_account(id),
image_url varchar NOT NULL,
active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
Loading

0 comments on commit a61389e

Please sign in to comment.