# -*- coding: utf-8 -*-
import unittest
from copy import deepcopy

from openprocurement.api.tests.base import snitch

from openprocurement.tender.belowthreshold.tests.base import test_organization

from openprocurement.tender.openeu.tests.base import (
    BaseTenderContentWebTest,
    test_tender_data,
    test_bids
)
from openprocurement.tender.openeu.tests.contract_blanks import (
    # TenderContractResourceTest
    contract_termination,
    patch_tender_contract,
)
from openprocurement.tender.belowthreshold.tests.contract_blanks import (
    # TenderContractResourceTest
    create_tender_contract_invalid,
    get_tender_contract,
    get_tender_contracts,
    # TenderContractDocumentResourceTest
    not_found,
    create_tender_contract_document,
    put_tender_contract_document,
    patch_tender_contract_document,
)
from openprocurement.tender.openua.tests.contract_blanks import (
    # TenderContractResourceTest
    create_tender_contract,
    patch_tender_contract_datesigned,
)


class TenderContractResourceTest(BaseTenderContentWebTest):
    #initial_data = tender_data
    initial_status = 'active.qualification'
    initial_bids = test_bids

    initial_auth = ('Basic', ('broker', ''))
    def setUp(self):
        super(TenderContractResourceTest, self).setUp()
        # Create award
        self.supplier_info = deepcopy(test_organization)
        self.app.authorization = ('Basic', ('token', ''))
        response = self.app.post_json('/tenders/{}/awards'.format(
            self.tender_id), {'data': {'suppliers': [self.supplier_info], 'status': 'pending', 'bid_id': self.initial_bids[0]['id'], 'value': {"amount": 500, "currency": "UAH", "valueAddedTaxIncluded": True}, 'items': test_tender_data["items"]}})
        award = response.json['data']
        self.award_id = award['id']
        self.app.authotization = ('Basic', ('broker', ''))
        response = self.app.patch_json('/tenders/{}/awards/{}'.format(self.tender_id, self.award_id), {"data": {"status": "active", "qualified": True, "eligible": True}})

    test_contract_termination = snitch(contract_termination)
    test_create_tender_contract_invalid = snitch(create_tender_contract_invalid)
    test_create_tender_contract = snitch(create_tender_contract)
    test_patch_tender_contract_datesigned = snitch(patch_tender_contract_datesigned)
    test_patch_tender_contract = snitch(patch_tender_contract)
    test_get_tender_contract = snitch(get_tender_contract)
    test_get_tender_contracts = snitch(get_tender_contracts)


class TenderContractDocumentResourceTest(BaseTenderContentWebTest):
    #initial_data = tender_data
    initial_status = 'active.qualification'
    initial_bids = test_bids
    test_status_that_denies_put_create_patch_contract_docs = 'unsuccessful'
    initial_auth = ('Basic', ('broker', ''))

    def setUp(self):
        super(TenderContractDocumentResourceTest, self).setUp()
        # Create award
        supplier_info = deepcopy(test_organization)
        self.app.authorization = ('Basic', ('token', ''))
        response = self.app.post_json('/tenders/{}/awards'.format(
            self.tender_id), {'data': {'suppliers': [supplier_info], 'status': 'pending', 'bid_id': self.initial_bids[0]['id']}})
        award = response.json['data']
        self.award_id = award['id']
        response = self.app.patch_json('/tenders/{}/awards/{}'.format(self.tender_id, self.award_id), {"data": {"status": "active", "qualified": True, "eligible": True}})
        # Create contract for award
        response = self.app.post_json('/tenders/{}/contracts'.format(self.tender_id), {'data': {'title': 'contract title', 'description': 'contract description', 'awardID': self.award_id}})
        contract = response.json['data']
        self.contract_id = contract['id']
        self.app.authorization = ('Basic', ('broker', ''))

    test_not_found = snitch(not_found)
    test_create_tender_contract_document = snitch(create_tender_contract_document)
    test_put_tender_contract_document = snitch(put_tender_contract_document)
    test_patch_tender_contract_document = snitch(patch_tender_contract_document)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TenderContractResourceTest))
    suite.addTest(unittest.makeSuite(TenderContractDocumentResourceTest))
    return suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')

@@ -190,17 +190,16 @@
 ization%0A
-%0A
 from ope
@@ -222,25 +222,37 @@
 der.
-openeu.tests.base
+belowthreshold.tests.contract
  imp
@@ -261,20 +261,16 @@
 t (%0A    
-Base
 TenderCo
@@ -275,58 +275,78 @@
 Cont
-entWebTest,%0A    test_tender_data,%0A    test_bids
+ractResourceTestMixin,%0A    TenderContractDocumentResourceTestMixin
 %0A)%0A
+%0A
 from
@@ -377,10 +377,10 @@
 open
-e
 u
+a
 .tes
@@ -445,35 +445,37 @@
 st%0A    c
-ontract_termination
+reate_tender_contract
 ,%0A    pa
@@ -493,20 +493,32 @@
 contract
+_datesigned
 ,%0A)%0A
+%0A
 from ope
@@ -541,336 +541,100 @@
 der.
-belowthreshold.tests.contract_blanks import (%0A    # TenderContractResourceTest%0A    create_tender_contract_invalid,%0A    get_tender_contract,%0A    get_tender_contracts,%0A    # TenderContractDocumentResource
+openeu.tests.base import (%0A    BaseTenderContentWeb
 Test
+,
 %0A    
-not_found,%0A    create_tender_contract_document,%0A    put_tender_contract_document,%0A    patch_tender_contract_document,
+test_tender_data,%0A    test_bids
 %0A)%0Af
@@ -656,34 +656,34 @@
 ment.tender.open
+e
 u
-a
 .tests.contract_
@@ -736,37 +736,35 @@
 st%0A    c
-reate_tender_contract
+ontract_termination
 ,%0A    pa
@@ -778,35 +778,24 @@
 der_contract
-_datesigned
 ,%0A)%0A%0A%0Aclass 
@@ -837,32 +837,65 @@
 erContentWebTest
+, TenderContractResourceTestMixin
 ):%0A    #initial_
@@ -986,17 +986,16 @@
 st_bids%0A
-%0A
     init
@@ -1027,24 +1027,25 @@
 oker', ''))%0A
+%0A
     def setU
@@ -1931,89 +1931,8 @@
 on)%0A
-    test_create_tender_contract_invalid = snitch(create_tender_contract_invalid)%0A
     
@@ -2144,128 +2144,8 @@
 ct)%0A
-    test_get_tender_contract = snitch(get_tender_contract)%0A    test_get_tender_contracts = snitch(get_tender_contracts)%0A
 %0A%0Acl
@@ -2207,16 +2207,57 @@
 tWebTest
+, TenderContractDocumentResourceTestMixin
 ):%0A    #
@@ -3511,289 +3511,8 @@
 ))%0A%0A
-    test_not_found = snitch(not_found)%0A    test_create_tender_contract_document = snitch(create_tender_contract_document)%0A    test_put_tender_contract_document = snitch(put_tender_contract_document)%0A    test_patch_tender_contract_document = snitch(patch_tender_contract_document)%0A%0A
 %0Adef

create basic abstract classes for contract module