# -*- coding: utf-8 -*-
import unittest

from openprocurement.api.tests.base import snitch
from openprocurement.api.tests.base import BaseWebTest

from openprocurement.tender.belowthreshold.tests.base import test_lots
from openprocurement.tender.belowthreshold.tests.tender import TenderResourceTestMixin
from openprocurement.tender.belowthreshold.tests.tender_blanks import (
    # TenderUAProcessTest
    invalid_tender_conditions,
)

from openprocurement.tender.openua.tests.tender import TenderUaProcessTestMixin
from openprocurement.tender.openua.tests.tender_blanks import (
    # TenderUAResourceTest
    empty_listing,
    create_tender_generated,
    tender_with_main_procurement_category,
    tender_finance_milestones,
)

from openprocurement.tender.openuadefense.tests.base import (
    BaseTenderUAWebTest,
    test_tender_data,
)
from openprocurement.tender.openuadefense.tests.tender_blanks import (
    # TenderUATest
    simple_add_tender,
    # TenderUAResourceTest
    create_tender_invalid,
    patch_tender,
    patch_tender_ua,
    # TenderUAProcessTest
    one_valid_bid_tender_ua,
    one_invalid_bid_tender,
)


class TenderUATest(BaseWebTest):

    initial_data = test_tender_data

    test_simple_add_tender = snitch(simple_add_tender)


class TenderUAResourceTest(BaseTenderUAWebTest, TenderResourceTestMixin):
    test_lots_data = test_lots  # TODO: change attribute identifier

    initial_data = test_tender_data

    test_empty_listing = snitch(empty_listing)
    test_create_tender_invalid = snitch(create_tender_invalid)
    test_create_tender_generated = snitch(create_tender_generated)
    test_patch_tender = snitch(patch_tender)
    test_patch_tender_ua = snitch(patch_tender_ua)
    test_tender_with_main_procurement_category = snitch(tender_with_main_procurement_category)
    test_tender_finance_milestones = snitch(tender_finance_milestones)


class TenderUAProcessTest(BaseTenderUAWebTest, TenderUaProcessTestMixin):
    initial_data = test_tender_data

    test_invalid_tender_conditions = snitch(invalid_tender_conditions)
    test_one_valid_bid_tender_ua = snitch(one_valid_bid_tender_ua)
    test_one_invalid_bid_tender = snitch(one_invalid_bid_tender)

    def test_patch_not_author(self):
        response = self.app.post_json('/tenders', {'data': test_tender_data})
        self.assertEqual(response.status, '201 Created')
        tender = response.json['data']
        owner_token = response.json['access']['token']

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

        response = self.app.post('/tenders/{}/documents'.format(tender['id']),
                                 upload_files=[('file', 'name.doc', 'content')])
        self.assertEqual(response.status, '201 Created')
        self.assertEqual(response.content_type, 'application/json')
        doc_id = response.json["data"]['id']
        self.assertIn(doc_id, response.headers['Location'])

        self.app.authorization = authorization
        response = self.app.patch_json('/tenders/{}/documents/{}?acc_token={}'.format(tender['id'], doc_id, owner_token),
                                       {"data": {"description": "document description"}}, status=403)
        self.assertEqual(response.status, '403 Forbidden')
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.json['errors'][0]["description"], "Can update document only author")

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TenderUAProcessTest))
    suite.addTest(unittest.makeSuite(TenderUAResourceTest))
    suite.addTest(unittest.makeSuite(TenderUATest))
    return suite


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

[((44, 29, 44, 54), 'openprocurement.api.tests.base.snitch', 'snitch', ({(44, 36, 44, 53): 'simple_add_tender'}, {}), '(simple_add_tender)', False, 'from openprocurement.api.tests.base import snitch\n'), ((52, 25, 52, 46), 'openprocurement.api.tests.base.snitch', 'snitch', ({(52, 32, 52, 45): 'empty_listing'}, {}), '(empty_listing)', False, 'from openprocurement.api.tests.base import snitch\n'), ((53, 33, 53, 62), 'openprocurement.api.tests.base.snitch', 'snitch', ({(53, 40, 53, 61): 'create_tender_invalid'}, {}), '(create_tender_invalid)', False, 'from openprocurement.api.tests.base import snitch\n'), ((54, 35, 54, 66), 'openprocurement.api.tests.base.snitch', 'snitch', ({(54, 42, 54, 65): 'create_tender_generated'}, {}), '(create_tender_generated)', False, 'from openprocurement.api.tests.base import snitch\n'), ((55, 24, 55, 44), 'openprocurement.api.tests.base.snitch', 'snitch', ({(55, 31, 55, 43): 'patch_tender'}, {}), '(patch_tender)', False, 'from openprocurement.api.tests.base import snitch\n'), ((56, 27, 56, 50), 'openprocurement.api.tests.base.snitch', 'snitch', ({(56, 34, 56, 49): 'patch_tender_ua'}, {}), '(patch_tender_ua)', False, 'from openprocurement.api.tests.base import snitch\n'), ((57, 49, 57, 94), 'openprocurement.api.tests.base.snitch', 'snitch', ({(57, 56, 57, 93): 'tender_with_main_procurement_category'}, {}), '(tender_with_main_procurement_category)', False, 'from openprocurement.api.tests.base import snitch\n'), ((58, 37, 58, 70), 'openprocurement.api.tests.base.snitch', 'snitch', ({(58, 44, 58, 69): 'tender_finance_milestones'}, {}), '(tender_finance_milestones)', False, 'from openprocurement.api.tests.base import snitch\n'), ((64, 37, 64, 70), 'openprocurement.api.tests.base.snitch', 'snitch', ({(64, 44, 64, 69): 'invalid_tender_conditions'}, {}), '(invalid_tender_conditions)', False, 'from openprocurement.api.tests.base import snitch\n'), ((65, 35, 65, 66), 'openprocurement.api.tests.base.snitch', 'snitch', ({(65, 42, 65, 65): 'one_valid_bid_tender_ua'}, {}), '(one_valid_bid_tender_ua)', False, 'from openprocurement.api.tests.base import snitch\n'), ((66, 34, 66, 64), 'openprocurement.api.tests.base.snitch', 'snitch', ({(66, 41, 66, 63): 'one_invalid_bid_tender'}, {}), '(one_invalid_bid_tender)', False, 'from openprocurement.api.tests.base import snitch\n'), ((92, 12, 92, 32), 'unittest.TestSuite', 'unittest.TestSuite', ({}, {}), '()', False, 'import unittest\n'), ((100, 4, 100, 38), 'unittest.main', 'unittest.main', (), '', False, 'import unittest\n'), ((93, 18, 93, 57), 'unittest.makeSuite', 'unittest.makeSuite', ({(93, 37, 93, 56): 'TenderUAProcessTest'}, {}), '(TenderUAProcessTest)', False, 'import unittest\n'), ((94, 18, 94, 58), 'unittest.makeSuite', 'unittest.makeSuite', ({(94, 37, 94, 57): 'TenderUAResourceTest'}, {}), '(TenderUAResourceTest)', False, 'import unittest\n'), ((95, 18, 95, 50), 'unittest.makeSuite', 'unittest.makeSuite', ({(95, 37, 95, 49): 'TenderUATest'}, {}), '(TenderUATest)', False, 'import unittest\n')]