github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/tests/v1/projects.test.js (about)

     1  const chai = require('chai');
     2  const chaiHttp = require('chai-http');
     3  const server = require('../../server');
     4  const ba = require('blockapps-rest');
     5  const util = ba.common.util;
     6  const common = ba.common;
     7  const should = ba.common.should;
     8  const assert = ba.common.assert;
     9  const expect = ba.common.expect;
    10  const config = common.config;
    11  const UserRole = ba.rest.getEnums(`${config.libPath}/user/contracts/UserRole.sol`).UserRole;
    12  const ProjectState = ba.rest.getEnums(`${config.libPath}/project/contracts/ProjectState.sol`).ProjectState;
    13  const BidState = ba.rest.getEnums(`${config.libPath}/bid/contracts/BidState.sol`).BidState;
    14  const ProjectEvent = ba.rest.getEnums(`${config.libPath}/project/contracts/ProjectEvent.sol`).ProjectEvent;
    15  
    16  chai.use(chaiHttp);
    17  
    18  describe("Projects Test", function() {
    19    const uid = util.uid();
    20    const projectArgs = createProjectArgs(uid);
    21    const supplier = "Supplier1";
    22    const amount = 100;
    23    let bidId;
    24  
    25    it('should create a project', function(done) {
    26      this.timeout(config.timeout);
    27      chai.request(server)
    28        .post('/api/v1/projects')
    29        .send(projectArgs)
    30        .end((err, res) => {
    31          const data = assert.apiData(err, res);
    32          const project = data.project;
    33          assert.isDefined(project, 'should return new project');
    34          // todo: the created project returns the created project
    35          assert.equal(project.name, projectArgs.name, 'new project should contain name');
    36          assert.equal(project.buyer, projectArgs.buyer, 'new project should contain buyer');
    37          assert.equal(project.description, projectArgs.description, 'project desc should be same as in request');
    38          assert.equal(project.spec, projectArgs.spec, 'project spec should be same as in request');
    39          done();
    40        });
    41    });
    42  
    43    it('should return a project by its name', function(done) {
    44      this.timeout(config.timeout);
    45      chai.request(server)
    46        .get(`/api/v1/projects/${encodeURIComponent(projectArgs.name)}/`)
    47        .end((err, res) => {
    48          const data = assert.apiData(err, res);
    49          const project = data.project;
    50          assert.isDefined(project, 'should return project');
    51          assert.equal(project.name, projectArgs.name, 'project name should be same as in request');
    52          assert.equal(project.buyer, projectArgs.buyer, 'new project should contain buyer');
    53          assert.equal(project.description, projectArgs.description, 'project desc should be same as in request');
    54          assert.equal(project.spec, projectArgs.spec, 'project spec should be same as in request');
    55          done();
    56        });
    57    });
    58  
    59    it('should return the list of projects filtered by buyer', function(done) {
    60      this.timeout(config.timeout);
    61  
    62      chai.request(server)
    63        .get('/api/v1/projects')
    64        .query(
    65          {
    66            filter: 'buyer',
    67            buyer: projectArgs.buyer,
    68          }
    69        )
    70        .end((err, res) => {
    71          const data = assert.apiData(err, res);
    72          const projects = data.projects;
    73          assert.isDefined(projects, 'should return projects');
    74          assert.isArray(projects, 'projects list should be an array');
    75          //todo: the returned list should be filtered by buyer (preliminarily create at least one project)
    76          done();
    77        });
    78    });
    79  
    80    it('should return the list of projects filtered by state', function(done) {
    81      this.timeout(config.timeout);
    82      const state = ProjectState.OPEN;
    83      chai.request(server)
    84        .get('/api/v1/projects')
    85        .query(
    86          {
    87            filter: 'state',
    88            state: state,
    89          }
    90        )
    91        .end((err, res) => {
    92          const data = assert.apiData(err, res);
    93          const projects = data.projects;
    94          assert.isDefined(projects, 'should return projects');
    95          assert.isArray(projects, 'projects list should be an array');
    96          assert.isOk(projects.length > 0, 'projects list should not be empty');
    97          //todo: the returned list should be filtered by state (preliminarily create at least one project for buyer)
    98          done();
    99        });
   100    });
   101  
   102    it('should return the list of projects filtered by supplier', function(done) {
   103      this.timeout(config.timeout);
   104      const supplier = "Supplier1";
   105      chai.request(server)
   106        .get('/api/v1/projects')
   107        .query(
   108          {
   109            filter: 'supplier',
   110            supplier: supplier,
   111          }
   112        )
   113        .end((err, res) => {
   114          assert.apiSuccess(res);
   115          res.body.should.have.property('data');
   116          const data = res.body.data;
   117          const projects = data.projects;
   118          assert.isDefined(projects, 'should return projects');
   119          assert.isArray(projects, 'projects list should be an array');
   120          //todo: the returned list should be filtered by supplier (preliminarily create at least one project for supplier)
   121          done();
   122        });
   123    });
   124  
   125    it('Should bid on a project', function(done){
   126      this.timeout(config.timeout);
   127      chai.request(server)
   128        .post(`/api/v1/projects/${encodeURIComponent(projectArgs.name)}/bids`)
   129        .send({ supplier, amount })
   130        .end((err, res) => {
   131          const data = assert.apiData(err, res);
   132          const bid = data.bid;
   133          assert.isDefined(bid, 'should return new bid');
   134          assert.equal(bid.supplier, supplier, 'new bid should contain supplier');
   135          assert.equal(bid.amount, amount, 'new bid should contain amount');
   136          bidId = bid.id; // save for the next tests
   137          done();
   138        });
   139    });
   140  
   141    it('Should get bids for a project', function(done){
   142      this.timeout(config.timeout);
   143      chai.request(server)
   144        .get(`/api/v1/projects/${encodeURIComponent(projectArgs.name)}/bids`)
   145        .end((err, res) => {
   146          const data = assert.apiData(err, res);
   147          const bids = data.bids;
   148          assert.isArray(bids, 'should be an array');
   149          assert.equal(bids.length, 1, 'length of bid array should be 1');
   150          const bid = bids[0];
   151          assert.isDefined(bid, 'should return new bid');
   152          assert.equal(bid.supplier, supplier, 'new bid should contain supplier');
   153          assert.equal(bid.amount, amount, 'new bid should contain amount');
   154          done();
   155        });
   156      });
   157  
   158    it('Should accept bid', function(done){
   159      this.timeout(config.timeout);
   160      chai.request(server)
   161        .post(`/api/v1/projects/${encodeURIComponent(projectArgs.name)}/events/`)
   162        .send({
   163          projectEvent: ProjectEvent.ACCEPT,
   164          username: projectArgs.buyer,
   165          bidId: bidId,
   166        })
   167        .end((err, res) => {
   168          assert.apiSuccess(res);
   169          res.body.should.have.property('data');
   170          // todo: body data should be empty
   171          // todo: check the project changed state to PRODUCTION
   172          done();
   173        });
   174    });
   175  
   176    it('should change project state to INTRANSIT', function(done) {
   177      this.timeout(config.timeout);
   178      chai.request(server)
   179        .post(`/api/v1/projects/${encodeURIComponent(projectArgs.name)}/events/`)
   180        .send({
   181          projectEvent: ProjectEvent.DELIVER,
   182          username: supplier,
   183        })
   184        .end((err, res) => {
   185          const data = assert.apiData(err, res); // FIXME -LS return value
   186          assert.equal(data.state, ProjectState.INTRANSIT, 'returned state should be INTRANSIT');
   187          done();
   188        });
   189    });
   190  
   191    // NOTE: in order to receive, a payment must be made.
   192    // to run properly, this test will requires the creation of users
   193    it.skip('should change project state to RECEIVED', function(done) {
   194      this.timeout(config.timeout);
   195      chai.request(server)
   196        .post('/api/v1/projects/' + projectArgs.name + '/events')
   197        .send({projectEvent: ProjectEvent.RECEIVE})
   198        .end((err, res) => {
   199          const data = assert.apiData(err, res); // FIXME -LS return value
   200          assert.equal(data.bid.state, ProjectState.RECEIVED, 'returned state should be RECEIVED');
   201          done();
   202        });
   203    });
   204  });
   205  
   206  
   207  function createProjectArgs(uid) {
   208    const projectArgs = {
   209      name: 'Project_ ? ' + uid,
   210      buyer: 'Buyer1',
   211      description: 'description_ ? % ' + uid,
   212      spec: 'spec_ ? % ' + uid,
   213      price: 1234,
   214  
   215      created: new Date().getTime(),
   216      targetDelivery: new Date().getTime() + 3 * 24*60*60*1000, // 3 days
   217    };
   218  
   219    return projectArgs;
   220  }