# -*- 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')

[((1237, 1262), 'openprocurement.api.tests.base.snitch', 'snitch', (['simple_add_tender'], {}), '(simple_add_tender)\n', (1243, 1262), False, 'from openprocurement.api.tests.base import snitch\n'), ((1470, 1491), 'openprocurement.api.tests.base.snitch', 'snitch', (['empty_listing'], {}), '(empty_listing)\n', (1476, 1491), False, 'from openprocurement.api.tests.base import snitch\n'), ((1525, 1554), 'openprocurement.api.tests.base.snitch', 'snitch', (['create_tender_invalid'], {}), '(create_tender_invalid)\n', (1531, 1554), False, 'from openprocurement.api.tests.base import snitch\n'), ((1590, 1621), 'openprocurement.api.tests.base.snitch', 'snitch', (['create_tender_generated'], {}), '(create_tender_generated)\n', (1596, 1621), False, 'from openprocurement.api.tests.base import snitch\n'), ((1646, 1666), 'openprocurement.api.tests.base.snitch', 'snitch', (['patch_tender'], {}), '(patch_tender)\n', (1652, 1666), False, 'from openprocurement.api.tests.base import snitch\n'), ((1694, 1717), 'openprocurement.api.tests.base.snitch', 'snitch', (['patch_tender_ua'], {}), '(patch_tender_ua)\n', (1700, 1717), False, 'from openprocurement.api.tests.base import snitch\n'), ((1767, 1812), 'openprocurement.api.tests.base.snitch', 'snitch', (['tender_with_main_procurement_category'], {}), '(tender_with_main_procurement_category)\n', (1773, 1812), False, 'from openprocurement.api.tests.base import snitch\n'), ((1850, 1883), 'openprocurement.api.tests.base.snitch', 'snitch', (['tender_finance_milestones'], {}), '(tender_finance_milestones)\n', (1856, 1883), False, 'from openprocurement.api.tests.base import snitch\n'), ((2034, 2067), 'openprocurement.api.tests.base.snitch', 'snitch', (['invalid_tender_conditions'], {}), '(invalid_tender_conditions)\n', (2040, 2067), False, 'from openprocurement.api.tests.base import snitch\n'), ((2103, 2134), 'openprocurement.api.tests.base.snitch', 'snitch', (['one_valid_bid_tender_ua'], {}), '(one_valid_bid_tender_ua)\n', (2109, 2134), False, 'from openprocurement.api.tests.base import snitch\n'), ((2169, 2199), 'openprocurement.api.tests.base.snitch', 'snitch', (['one_invalid_bid_tender'], {}), '(one_invalid_bid_tender)\n', (2175, 2199), False, 'from openprocurement.api.tests.base import snitch\n'), ((3493, 3513), 'unittest.TestSuite', 'unittest.TestSuite', ([], {}), '()\n', (3511, 3513), False, 'import unittest\n'), ((3735, 3769), 'unittest.main', 'unittest.main', ([], {'defaultTest': '"""suite"""'}), "(defaultTest='suite')\n", (3748, 3769), False, 'import unittest\n'), ((3532, 3571), 'unittest.makeSuite', 'unittest.makeSuite', (['TenderUAProcessTest'], {}), '(TenderUAProcessTest)\n', (3550, 3571), False, 'import unittest\n'), ((3591, 3631), 'unittest.makeSuite', 'unittest.makeSuite', (['TenderUAResourceTest'], {}), '(TenderUAResourceTest)\n', (3609, 3631), False, 'import unittest\n'), ((3651, 3683), 'unittest.makeSuite', 'unittest.makeSuite', (['TenderUATest'], {}), '(TenderUATest)\n', (3669, 3683), False, 'import unittest\n')]