def test_docs_2pc(self):
        self.app.authorization = ('Basic', ('broker', ''))

        # Creating tender in draft status

        for item in test_tender_data['items']:
            item['deliveryDate'] = {
                "startDate": (get_now() + timedelta(days=2)).isoformat(),
                "endDate": (get_now() + timedelta(days=5)).isoformat()
            }

        test_tender_data.update({
            "enquiryPeriod": {
                "endDate": (get_now() + timedelta(days=7)).isoformat()
            },
            "tenderPeriod": {
                "endDate": (get_now() + timedelta(days=14)).isoformat()
            }
        })

        data = test_tender_data.copy()
        data['status'] = 'draft'

        with open(TARGET_DIR + 'tutorial/tender-post-2pc.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders?opt_pretty=1',
                                          {'data': data})
            self.assertEqual(response.status, '201 Created')

        tender = response.json['data']
        self.tender_id = tender['id']
        owner_token = response.json['access']['token']

        # switch to 'active.enquiries'

        with open(TARGET_DIR + 'tutorial/tender-patch-2pc.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(tender['id'], owner_token),
                {'data': {
                    "status": 'active.enquiries'
                }})
            self.assertEqual(response.status, '200 OK')
Ejemplo n.º 2
0
    def test_docs(self):
        self.app.authorization = ('Basic', ('broker', ''))
        # empty tenders listing
        response = self.app.get('/tenders')
        self.assertEqual(response.json['data'], [])
        # create tender
        test_tender_data['items'].append(deepcopy(
            test_tender_data['items'][0]))
        for item in test_tender_data['items']:
            item['deliveryDate'] = {
                "startDate": (get_now() + timedelta(days=2)).isoformat(),
                "endDate": (get_now() + timedelta(days=5)).isoformat()
            }
        test_tender_data.update({
            "enquiryPeriod": {
                "endDate": (get_now() + timedelta(days=7)).isoformat()
            },
            "tenderPeriod": {
                "endDate": (get_now() + timedelta(days=14)).isoformat()
            }
        })
        response = self.app.post_json('/tenders', {"data": test_tender_data})
        tender_id = self.tender_id = response.json['data']['id']
        owner_token = response.json['access']['token']
        # switch to active.tendering
        response = self.set_status(
            'active.tendering', {
                "auctionPeriod": {
                    "startDate": (get_now() + timedelta(days=10)).isoformat()
                }
            })
        self.assertIn("auctionPeriod", response.json['data'])
        # create bid
        self.app.authorization = ('Basic', ('broker', ''))
        response = self.app.post_json('/tenders/{}/bids'.format(tender_id), {
            'data': {
                'tenderers': [test_organization],
                "value": {
                    "amount": 500
                }
            }
        })
        # switch to active.qualification
        self.set_status('active.auction', {'status': 'active.tendering'})
        self.app.authorization = ('Basic', ('chronograph', ''))
        response = self.app.patch_json('/tenders/{}'.format(tender_id),
                                       {'data': {
                                           "id": tender_id
                                       }})
        self.assertNotIn('auctionPeriod', response.json['data'])
        # get awards
        self.app.authorization = ('Basic', ('broker', ''))
        response = self.app.get('/tenders/{}/awards?acc_token={}'.format(
            tender_id, owner_token))
        # get pending award
        award_id = [
            i['id'] for i in response.json['data'] if i['status'] == 'pending'
        ][0]
        # set award as active
        self.app.patch_json(
            '/tenders/{}/awards/{}?acc_token={}'.format(
                tender_id, award_id, owner_token),
            {"data": {
                "status": "active"
            }})
        # get contract id
        response = self.app.get('/tenders/{}'.format(tender_id))
        contract_id = response.json['data']['contracts'][-1]['id']
        # after stand slill period
        self.app.authorization = ('Basic', ('chronograph', ''))
        self.set_status('complete', {'status': 'active.awarded'})
        self.tick()
        # time travel
        tender = self.db.get(tender_id)
        for i in tender.get('awards', []):
            i['complaintPeriod']['endDate'] = i['complaintPeriod']['startDate']
        self.db.save(tender)
        # sign contract
        self.app.authorization = ('Basic', ('broker', ''))
        self.app.patch_json(
            '/tenders/{}/contracts/{}?acc_token={}'.format(
                tender_id, contract_id, owner_token),
            {"data": {
                "status": "active",
                "value": {
                    "amountNet": 490
                }
            }})
        # check status
        self.app.authorization = ('Basic', ('broker', ''))
        with open(TARGET_DIR + 'example_tender.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}'.format(tender_id))
            self.assertEqual(response.json['data']['status'], 'complete')

        with open(TARGET_DIR + 'example_contract.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/contracts/{}'.format(
                tender_id, response.json['data']['contracts'][0]['id']))
        test_contract_data = response.json['data']

        request_path = '/contracts'

        #### Exploring basic rules

        with open(TARGET_DIR + 'contracts-listing-0.http',
                  'w') as self.app.file_obj:
            self.app.authorization = None
            response = self.app.get(request_path)
            self.assertEqual(response.status, '200 OK')
            self.app.file_obj.write("\n")

        #### Sync contract (i.e. simulate contracting databridge sync actions)
        self.app.authorization = ('Basic', ('contracting', ''))

        response = self.app.get(
            '/tenders/{}/extract_credentials'.format(tender_id))
        test_contract_data['owner'] = response.json['data']['owner']
        test_contract_data['tender_token'] = response.json['data'][
            'tender_token']
        test_contract_data['tender_id'] = tender_id
        test_contract_data['procuringEntity'] = tender['procuringEntity']
        del test_contract_data['status']

        response = self.app.post_json(request_path,
                                      {'data': test_contract_data})
        self.assertEqual(response.status, '201 Created')
        self.assertEqual(response.json['data']['status'], 'active')

        # Getting contract
        self.app.authorization = None

        with open(TARGET_DIR + 'contract-view.http', 'w') as self.app.file_obj:
            response = self.app.get('/contracts/{}'.format(
                test_contract_data['id']))
            self.assertEqual(response.status, '200 OK')
            contract = response.json['data']
            self.assertEqual(contract['status'], 'active')

        # Getting access
        self.app.authorization = ('Basic', ('broker', ''))
        with open(TARGET_DIR + 'contract-credentials.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}/credentials?acc_token={}'.format(
                    test_contract_data['id'], owner_token))
            self.assertEqual(response.status, '200 OK')
        self.app.get(request_path)
        contract_token = response.json['access']['token']
        contract_id = test_contract_data['id']

        with open(TARGET_DIR + 'contracts-listing-1.http',
                  'w') as self.app.file_obj:
            response = self.app.get(request_path)
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(len(response.json['data']), 1)

        # Modifying contract

        # Submitting contract change add contract change
        with open(TARGET_DIR + 'add-contract-change.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/contracts/{}/changes?acc_token={}'.format(
                    contract_id, contract_token), {
                        'data': {
                            'rationale': u'Опис причини змін контракту',
                            'rationale_en': 'Contract change cause',
                            'rationaleTypes': ['volumeCuts', 'priceReduction']
                        }
                    })
            self.assertEqual(response.status, '201 Created')
            self.assertEqual(response.content_type, 'application/json')
            change = response.json['data']

        with open(TARGET_DIR + 'view-contract-change.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/contracts/{}/changes/{}'.format(
                contract_id, change['id']))
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(response.json['data']['id'], change['id'])

        with open(TARGET_DIR + 'patch-contract-change.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}/changes/{}?acc_token={}'.format(
                    contract_id, change['id'], contract_token), {
                        'data': {
                            'rationale':
                            u'Друга і третя поставка має бути розфасована'
                        }
                    })
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(response.content_type, 'application/json')
            change = response.json['data']

        # add contract change document
        with open(TARGET_DIR + 'add-contract-change-document.http',
                  'w') as self.app.file_obj:
            response = self.app.post(
                '/contracts/{}/documents?acc_token={}'.format(
                    contract_id, contract_token),
                upload_files=[('file', 'contract_changes.doc', 'content')])
            self.assertEqual(response.status, '201 Created')
            self.assertEqual(response.content_type, 'application/json')
            doc_id = response.json["data"]['id']

        with open(TARGET_DIR + 'set-document-of-change.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}/documents/{}?acc_token={}'.format(
                    contract_id, doc_id, contract_token), {
                        'data': {
                            "documentOf": "change",
                            "relatedItem": change['id'],
                        }
                    })
            self.assertEqual(response.status, '200 OK')

        # updating contract properties
        with open(TARGET_DIR + 'contracts-patch.http',
                  'w') as self.app.file_obj:
            custom_period_start_date = get_now().isoformat()
            custom_period_end_date = (get_now() +
                                      timedelta(days=30)).isoformat()
            response = self.app.patch_json(
                '/contracts/{}?acc_token={}'.format(contract_id,
                                                    contract_token),
                {
                    "data": {
                        "value": {
                            "amount": 438,
                            "amountNet": 430
                        },
                        "period": {
                            'startDate': custom_period_start_date,
                            'endDate': custom_period_end_date
                        }
                    }
                })
            self.assertEqual(response.status, '200 OK')

        # update item
        with open(TARGET_DIR + 'update-contract-item.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}?acc_token={}'.format(contract_id,
                                                    contract_token),
                {
                    "data": {
                        "items": [{
                            'quantity': 2
                        }, {}]
                    },
                })
            self.assertEqual(response.status, '200 OK')
            item2 = response.json['data']['items'][0]
            self.assertEqual(item2['quantity'], 2)

        # delete item
        with open(TARGET_DIR + 'delete-contract-item.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}?acc_token={}'.format(contract_id,
                                                    contract_token), {
                                                        "data": {
                                                            "items": [item2]
                                                        },
                                                    })
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(len(response.json['data']['items']), 1)

        # apply contract change
        with open(TARGET_DIR + 'apply-contract-change.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}/changes/{}?acc_token={}'.format(
                    contract_id, change['id'], contract_token), {
                        'data': {
                            'status': 'active',
                            'dateSigned': get_now().isoformat()
                        }
                    })
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(response.content_type, 'application/json')

        with open(TARGET_DIR + 'view-all-contract-changes.http',
                  'w') as self.app.file_obj:
            response = self.app.get(
                '/contracts/{}/changes'.format(contract_id))
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(len(response.json['data']), 1)

        with open(TARGET_DIR + 'view-contract.http', 'w') as self.app.file_obj:
            response = self.app.get('/contracts/{}'.format(contract_id))
            self.assertEqual(response.status, '200 OK')
            self.assertIn('changes', response.json['data'])

        # Uploading documentation
        with open(TARGET_DIR + 'upload-contract-document.http',
                  'w') as self.app.file_obj:
            response = self.app.post(
                '/contracts/{}/documents?acc_token={}'.format(
                    contract_id, contract_token),
                upload_files=[('file', u'contract.doc', 'content')])

        with open(TARGET_DIR + 'contract-documents.http',
                  'w') as self.app.file_obj:
            response = self.app.get(
                '/contracts/{}/documents?acc_token={}'.format(
                    contract_id, contract_token))

        with open(TARGET_DIR + 'upload-contract-document-2.http',
                  'w') as self.app.file_obj:
            response = self.app.post(
                '/contracts/{}/documents?acc_token={}'.format(
                    contract_id, contract_token),
                upload_files=[('file', u'contract_additional_docs.doc',
                               'additional info')])

        doc_id = response.json['data']['id']

        with open(TARGET_DIR + 'upload-contract-document-3.http',
                  'w') as self.app.file_obj:
            response = self.app.put(
                '/contracts/{}/documents/{}?acc_token={}'.format(
                    contract_id, doc_id, contract_token),
                upload_files=[('file', 'contract_additional_docs.doc',
                               'extended additional info')])

        with open(TARGET_DIR + 'get-contract-document-3.http',
                  'w') as self.app.file_obj:
            response = self.app.get(
                '/contracts/{}/documents/{}?acc_token={}'.format(
                    contract_id, doc_id, contract_token))

        # Finalize contract
        with open(TARGET_DIR + 'contract-termination.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/contracts/{}?acc_token={}'.format(contract_id,
                                                    contract_token),
                {
                    "data": {
                        "status": "terminated",
                        "amountPaid": {
                            "amount": 430,
                            "amountNet": 420
                        }
                    }
                })
            self.assertEqual(response.status, '200 OK')
    def test_docs_tutorial(self):

        request_path = '/tenders?opt_pretty=1'

        # Exploring basic rules

        with open(TARGET_DIR + 'tutorial/tender-listing.http',
                  'w') as self.app.file_obj:
            self.app.authorization = ('Basic', ('broker', ''))
            response = self.app.get('/tenders')
            self.assertEqual(response.status, '200 OK')
            self.app.file_obj.write("\n")

        with open(TARGET_DIR + 'tutorial/tender-post-attempt.http',
                  'w') as self.app.file_obj:
            response = self.app.post(request_path, 'data', status=415)
            self.assertEqual(response.status, '415 Unsupported Media Type')

        self.app.authorization = ('Basic', ('broker', ''))

        with open(TARGET_DIR + 'tutorial/tender-post-attempt-json.http',
                  'w') as self.app.file_obj:
            self.app.authorization = ('Basic', ('broker', ''))
            response = self.app.post(request_path,
                                     'data',
                                     content_type='application/json',
                                     status=422)
            self.assertEqual(response.status, '422 Unprocessable Entity')

        # Creating tender

        for item in test_tender_data['items']:
            item['deliveryDate'] = {
                "startDate": (get_now() + timedelta(days=2)).isoformat(),
                "endDate": (get_now() + timedelta(days=5)).isoformat()
            }

        test_tender_data.update({
            "enquiryPeriod": {
                "endDate": (get_now() + timedelta(days=7)).isoformat()
            },
            "tenderPeriod": {
                "endDate": (get_now() + timedelta(days=14)).isoformat()
            }
        })

        with open(TARGET_DIR + 'tutorial/tender-post-attempt-json-data.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders?opt_pretty=1',
                                          {'data': test_tender_data})
            self.assertEqual(response.status, '201 Created')

        tender = response.json['data']
        owner_token = response.json['access']['token']
        self.tender_id = tender['id']

        self.set_status('active.enquiries')

        with open(TARGET_DIR + 'tutorial/blank-tender-view.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}'.format(tender['id']))
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/initial-tender-listing.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders')
            self.assertEqual(response.status, '200 OK')

        tender_below_maximum['items'][0]['id'] = uuid4().hex
        for feature in tender_below_maximum['features']:
            if feature['featureOf'] == 'item':
                feature['relatedItem'] = tender_below_maximum['items'][0]['id']

        with open(TARGET_DIR + 'tutorial/create-tender-procuringEntity.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders?opt_pretty=1',
                                          {'data': tender_below_maximum})
            self.assertEqual(response.status, '201 Created')

        test_tender_funders_data = deepcopy(test_tender_data)
        test_tender_funders_data['funders'] = [funder]
        with open(TARGET_DIR + 'tutorial/create-tender-funders.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders?opt_pretty=1',
                                          {'data': test_tender_funders_data})
            self.assertEqual(response.status, '201 Created')

        response = self.app.post_json('/tenders?opt_pretty=1',
                                      {'data': test_tender_data})
        self.assertEqual(response.status, '201 Created')

        with open(
                TARGET_DIR +
                'tutorial/tender-listing-after-procuringEntity.http',
                'w') as self.app.file_obj:
            response = self.app.get('/tenders')
            self.assertEqual(response.status, '200 OK')

        self.app.authorization = ('Basic', ('broker', ''))

        # Modifying tender

        self.tick()

        tender_period_end_date = get_now() + timedelta(days=15, seconds=10)
        with open(TARGET_DIR + 'tutorial/patch-items-value-periods.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(tender['id'], owner_token), {
                    'data': {
                        "tenderPeriod": {
                            "endDate": tender_period_end_date.isoformat()
                        }
                    }
                })

        with open(TARGET_DIR + 'tutorial/tender-listing-after-patch.http',
                  'w') as self.app.file_obj:
            self.app.authorization = None
            response = self.app.get(request_path)
            self.assertEqual(response.status, '200 OK')

        self.app.authorization = ('Basic', ('broker', ''))

        # Setting funders

        with open(TARGET_DIR + 'tutorial/patch-tender-funders.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(tender['id'], owner_token),
                {'data': {
                    "funders": [funder]
                }})
            self.assertIn('funders', response.json['data'])
            self.assertEqual(response.status, '200 OK')

        # Setting Bid guarantee

        with open(TARGET_DIR + 'tutorial/set-bid-guarantee.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(self.tender_id, owner_token),
                {'data': {
                    "guarantee": {
                        "amount": 8,
                        "currency": "USD"
                    }
                }})
            self.assertEqual(response.status, '200 OK')
            self.assertIn('guarantee', response.json['data'])

        # Uploading documentation

        with open(TARGET_DIR + 'tutorial/upload-tender-notice.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/documents?acc_token={}'.format(
                    self.tender_id, owner_token), {
                        'data': {
                            'title': u'Notice.pdf',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/pdf',
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        doc_id = response.json["data"]["id"]
        with open(TARGET_DIR + 'tutorial/tender-documents.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/documents/{}'.format(
                self.tender_id, doc_id))
            self.assertEqual(response.status, '200 OK')

        with open(
                TARGET_DIR + 'tutorial/tender-document-add-documentType.http',
                'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/documents/{}?acc_token={}'.format(
                    self.tender_id, doc_id, owner_token),
                {'data': {
                    "documentType": "technicalSpecifications"
                }})
            self.assertEqual(response.status, '200 OK')

        with open(
                TARGET_DIR + 'tutorial/tender-document-edit-docType-desc.http',
                'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/documents/{}?acc_token={}'.format(
                    self.tender_id, doc_id, owner_token),
                {'data': {
                    "description": "document description modified"
                }})
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/upload-award-criteria.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/documents?acc_token={}'.format(
                    self.tender_id, owner_token), {
                        'data': {
                            'title': u'AwardCriteria.pdf',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/pdf',
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        doc_id = response.json["data"]["id"]

        with open(TARGET_DIR + 'tutorial/tender-documents-2.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/documents'.format(
                self.tender_id))
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/update-award-criteria.http',
                  'w') as self.app.file_obj:
            response = self.app.put_json(
                '/tenders/{}/documents/{}?acc_token={}'.format(
                    self.tender_id, doc_id, owner_token), {
                        'data': {
                            'title': u'AwardCriteria-2.pdf',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/pdf',
                        }
                    })
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/tender-documents-3.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/documents'.format(
                self.tender_id))
            self.assertEqual(response.status, '200 OK')

        # Enquiries

        with open(TARGET_DIR + 'tutorial/ask-question.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders/{}/questions'.format(
                self.tender_id), {"data": question},
                                          status=201)
            question_id = response.json['data']['id']
            self.assertEqual(response.status, '201 Created')

        with open(TARGET_DIR + 'tutorial/answer-question.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/questions/{}?acc_token={}'.format(
                    self.tender_id, question_id, owner_token),
                {
                    "data": {
                        "answer": "Таблицю додано в файлі \"Kalorijnist.xslx\""
                    }
                },
                status=200)
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/list-question.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/questions'.format(
                self.tender_id))
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/get-answer.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/questions/{}'.format(
                self.tender_id, question_id))
            self.assertEqual(response.status, '200 OK')

        # Registering bid

        self.set_status('active.tendering')

        self.app.authorization = ('Basic', ('broker', ''))
        bids_access = {}
        with open(TARGET_DIR + 'tutorial/register-bidder.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/bids'.format(self.tender_id), {'data': bid_draft})
            bid1_id = response.json['data']['id']
            bids_access[bid1_id] = response.json['access']['token']
            self.assertEqual(response.status, '201 Created')

        with open(TARGET_DIR + 'tutorial/activate-bidder.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/bids/{}?acc_token={}'.format(
                    self.tender_id, bid1_id, bids_access[bid1_id]),
                {'data': {
                    "status": "active"
                }})
            self.assertEqual(response.status, '200 OK')

        # Proposal Uploading

        with open(TARGET_DIR + 'tutorial/upload-bid-proposal.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/bids/{}/documents?acc_token={}'.format(
                    self.tender_id, bid1_id, bids_access[bid1_id]), {
                        'data': {
                            'title': u'Proposal.pdf',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/pdf',
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        with open(TARGET_DIR + 'tutorial/bidder-documents.http',
                  'w') as self.app.file_obj:
            response = self.app.get(
                '/tenders/{}/bids/{}/documents?acc_token={}'.format(
                    self.tender_id, bid1_id, bids_access[bid1_id]))
            self.assertEqual(response.status, '200 OK')

        # Second bid registration with documents

        with open(TARGET_DIR + 'tutorial/register-2nd-bidder.http',
                  'w') as self.app.file_obj:
            for document in bid2_with_docs['documents']:
                document['url'] = self.generate_docservice_url()
            response = self.app.post_json(
                '/tenders/{}/bids'.format(self.tender_id),
                {'data': bid2_with_docs})
            bid2_id = response.json['data']['id']
            bids_access[bid2_id] = response.json['access']['token']
            self.assertEqual(response.status, '201 Created')

        # Auction

        self.set_status('active.auction')
        self.app.authorization = ('Basic', ('auction', ''))
        auction_url = u'{}/tenders/{}'.format(self.auctions_url,
                                              self.tender_id)
        patch_data = {
            'auctionUrl':
            auction_url,
            'bids': [{
                "id":
                bid1_id,
                "participationUrl":
                u'{}?key_for_bid={}'.format(auction_url, bid1_id)
            }, {
                "id":
                bid2_id,
                "participationUrl":
                u'{}?key_for_bid={}'.format(auction_url, bid2_id)
            }]
        }
        response = self.app.patch_json(
            '/tenders/{}/auction?acc_token={}'.format(self.tender_id,
                                                      owner_token),
            {'data': patch_data})
        self.assertEqual(response.status, '200 OK')

        self.app.authorization = ('Basic', ('broker', ''))

        with open(TARGET_DIR + 'tutorial/auction-url.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}'.format(self.tender_id))
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/bidder-participation-url.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/bids/{}?acc_token={}'.format(
                self.tender_id, bid1_id, bids_access[bid1_id]))
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/bidder2-participation-url.http',
                  'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/bids/{}?acc_token={}'.format(
                self.tender_id, bid2_id, bids_access[bid2_id]))
            self.assertEqual(response.status, '200 OK')

        # Confirming qualification

        self.app.authorization = ('Basic', ('auction', ''))
        response = self.app.get('/tenders/{}/auction'.format(self.tender_id))
        auction_bids_data = response.json['data']['bids']
        response = self.app.post_json(
            '/tenders/{}/auction'.format(self.tender_id),
            {'data': {
                'bids': auction_bids_data
            }})

        self.app.authorization = ('Basic', ('broker', ''))

        response = self.app.get('/tenders/{}/awards'.format(self.tender_id))

        # get pending award
        award_id = [
            i['id'] for i in response.json['data'] if i['status'] == 'pending'
        ][0]

        with open(TARGET_DIR + 'tutorial/confirm-qualification.http',
                  'w') as self.app.file_obj:
            self.app.patch_json(
                '/tenders/{}/awards/{}?acc_token={}'.format(
                    self.tender_id, award_id, owner_token),
                {"data": {
                    "status": "active"
                }})
            self.assertEqual(response.status, '200 OK')

        response = self.app.get('/tenders/{}/contracts'.format(self.tender_id))
        self.contract_id = response.json['data'][0]['id']

        ####  Set contract value

        with open(
                TARGET_DIR +
                'tutorial/tender-contract-get-contract-value.http',
                'w') as self.app.file_obj:
            response = self.app.get('/tenders/{}/contracts/{}'.format(
                self.tender_id, self.contract_id))
        self.assertEqual(response.status, '200 OK')

        tender = self.db.get(self.tender_id)
        for i in tender.get('awards', []):
            i['complaintPeriod']['endDate'] = i['complaintPeriod']['startDate']
        self.db.save(tender)

        with open(
                TARGET_DIR +
                'tutorial/tender-contract-set-contract-value.http',
                'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/contracts/{}?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token), {
                        "data": {
                            "contractNumber": "contract #13111",
                            "value": {
                                "amount": 238,
                                "amountNet": 230
                            }
                        }
                    })
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.json['data']['value']['amount'], 238)

        #### Setting contract signature date

        self.tick()

        with open(TARGET_DIR + 'tutorial/tender-contract-sign-date.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/contracts/{}?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token),
                {'data': {
                    "dateSigned": get_now().isoformat()
                }})
            self.assertEqual(response.status, '200 OK')

        #### Setting contract period

        period_dates = {
            "period": {
                "startDate": get_now().isoformat(),
                "endDate": (get_now() + timedelta(days=365)).isoformat()
            }
        }
        with open(TARGET_DIR + 'tutorial/tender-contract-period.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/contracts/{}?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token),
                {'data': {
                    'period': period_dates["period"]
                }})
        self.assertEqual(response.status, '200 OK')

        #### Uploading contract documentation

        with open(TARGET_DIR + 'tutorial/tender-contract-upload-document.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/contracts/{}/documents?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token), {
                        'data': {
                            'title': u'contract_first_document.doc',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/msword',
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        with open(TARGET_DIR + 'tutorial/tender-contract-get-documents.http',
                  'w') as self.app.file_obj:
            response = self.app.get(
                '/tenders/{}/contracts/{}/documents'.format(
                    self.tender_id, self.contract_id))
        self.assertEqual(response.status, '200 OK')

        with open(
                TARGET_DIR +
                'tutorial/tender-contract-upload-second-document.http',
                'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/contracts/{}/documents?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token), {
                        'data': {
                            'title': u'contract_second_document.doc',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/msword',
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        with open(
                TARGET_DIR +
                'tutorial/tender-contract-get-documents-again.http',
                'w') as self.app.file_obj:
            response = self.app.get(
                '/tenders/{}/contracts/{}/documents'.format(
                    self.tender_id, self.contract_id))
        self.assertEqual(response.status, '200 OK')

        #### Setting contract signature date

        with open(TARGET_DIR + 'tutorial/tender-contract-sign-date.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/contracts/{}?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token),
                {'data': {
                    "dateSigned": get_now().isoformat()
                }})
            self.assertEqual(response.status, '200 OK')

        #### Contract signing

        tender = self.db.get(self.tender_id)
        for i in tender.get('awards', []):
            i['complaintPeriod']['endDate'] = i['complaintPeriod']['startDate']
        self.db.save(tender)

        with open(TARGET_DIR + 'tutorial/tender-contract-sign.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/contracts/{}?acc_token={}'.format(
                    self.tender_id, self.contract_id, owner_token),
                {'data': {
                    'status': 'active'
                }})
            self.assertEqual(response.status, '200 OK')

        # Preparing the cancellation request

        self.set_status('active.awarded')
        with open(TARGET_DIR + 'tutorial/prepare-cancellation.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/cancellations?acc_token={}'.format(
                    self.tender_id, owner_token), {
                        'data': {
                            'reason': 'cancellation reason',
                            'reasonType': 'noDemand'
                        }
                    })
            self.assertEqual(response.status, '201 Created')

        cancellation_id = response.json['data']['id']

        # Filling cancellation with protocol and supplementary documentation

        with open(TARGET_DIR + 'tutorial/upload-cancellation-doc.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json(
                '/tenders/{}/cancellations/{}/documents?acc_token={}'.format(
                    self.tender_id, cancellation_id, owner_token), {
                        'data': {
                            'title': u'Notice.pdf',
                            'url': self.generate_docservice_url(),
                            'hash': 'md5:' + '0' * 32,
                            'format': 'application/pdf',
                        }
                    })
            cancellation_doc_id = response.json['data']['id']
            self.assertEqual(response.status, '201 Created')

        with open(TARGET_DIR + 'tutorial/patch-cancellation.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/cancellations/{}/documents/{}?acc_token={}'.
                format(self.tender_id, cancellation_id, cancellation_doc_id,
                       owner_token),
                {'data': {
                    "description": 'Changed description'
                }})
            self.assertEqual(response.status, '200 OK')

        with open(TARGET_DIR + 'tutorial/update-cancellation-doc.http',
                  'w') as self.app.file_obj:
            response = self.app.put_json(
                '/tenders/{}/cancellations/{}/documents/{}?acc_token={}'.
                format(self.tender_id, cancellation_id, cancellation_doc_id,
                       owner_token), {
                           'data': {
                               'title': u'Notice-2.pdf',
                               'url': self.generate_docservice_url(),
                               'hash': 'md5:' + '0' * 32,
                               'format': 'application/pdf',
                           }
                       })
            self.assertEqual(response.status, '200 OK')

        # Activating the request and cancelling tender

        with open(TARGET_DIR + 'tutorial/active-cancellation.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}/cancellations/{}?acc_token={}'.format(
                    self.tender_id, cancellation_id, owner_token),
                {'data': {
                    "status": "active"
                }})
            self.assertEqual(response.status, '200 OK')
    def test_docs_milestones(self):
        self.app.authorization = ('Basic', ('broker', ''))

        for item in test_tender_data['items']:
            item['deliveryDate'] = {
                "startDate": (get_now() + timedelta(days=2)).isoformat(),
                "endDate": (get_now() + timedelta(days=5)).isoformat()
            }

        test_tender_data.update({
            "enquiryPeriod": {
                "endDate": (get_now() + timedelta(days=7)).isoformat()
            },
            "tenderPeriod": {
                "endDate": (get_now() + timedelta(days=14)).isoformat()
            }
        })

        data = dict(**test_tender_data)
        data["milestones"] = [
            {
                'title': "signingTheContract",
                'code': 'prepayment',
                'type': 'financing',
                'duration': {
                    'days': 5,
                    'type': 'banking'
                },
                'sequenceNumber': 0,
                'percentage': 45.55,
            },
            {
                'title': "deliveryOfGoods",
                'code': 'postpayment',
                'type': 'financing',
                'duration': {
                    'days': 7,
                    'type': 'calendar'
                },
                'sequenceNumber': 1,
                'percentage': 54.45,
            },
        ]
        with open(TARGET_DIR + 'milestones/tender-post-milestones.http',
                  'w') as self.app.file_obj:
            response = self.app.post_json('/tenders', {'data': data})
        self.assertEqual(response.status, '201 Created')

        tender = response.json['data']
        owner_token = response.json['access']['token']

        with open(TARGET_DIR + 'milestones/tender-patch-milestones.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(tender["id"], owner_token), {
                    "data": {
                        "milestones": [{}, {
                            "title": "anotherEvent",
                            "description": u"Підозрілий опис",
                        }]
                    }
                })
            self.assertEqual(response.status, '200 OK')

        response = self.app.post_json(
            '/tenders/{}/lots?acc_token={}'.format(tender["id"], owner_token),
            {'data': test_lots[0]})
        self.assertEqual(response.status, '201 Created')
        lot = response.json["data"]

        with open(TARGET_DIR + 'milestones/tender-patch-lot-milestones.http',
                  'w') as self.app.file_obj:
            response = self.app.patch_json(
                '/tenders/{}?acc_token={}'.format(tender["id"], owner_token), {
                    "data": {
                        "milestones": [{
                            "relatedLot": lot["id"]
                        }, {
                            "relatedLot": lot["id"]
                        }]
                    }
                })
            self.assertEqual(response.status, '200 OK')

        with open(
                TARGET_DIR +
                'milestones/tender-delete-lot-milestones-error.http',
                'w') as self.app.file_obj:
            response = self.app.delete(
                '/tenders/{}/lots/{}?acc_token={}'.format(
                    tender["id"], lot['id'], owner_token),
                status=422)
        self.assertEqual(response.status, '422 Unprocessable Entity')
        self.assertIn(
            {
                "location":
                "body",
                "name":
                "milestones",
                "description": [{
                    "relatedLot": ["relatedLot should be one of the lots."]
                }, {
                    "relatedLot": ["relatedLot should be one of the lots."]
                }]
            }, response.json['errors'])