from django.test import TestCase
from django.test.testcases import SimpleTestCase
from django.test.utils import override_settings
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users
from corehq.apps.domain.models import Domain
from casexml.apps.case.tests.util import (
    delete_all_cases,
    delete_all_sync_logs,
)
from casexml.apps.case.mock import CaseBlock
from casexml.apps.phone.restore import FileRestoreResponse
from casexml.apps.phone.tests.utils import create_restore_user, MockDevice


@override_settings(CASEXML_FORCE_DOMAIN_CHECK=False)
class OtaV3RestoreTest(TestCase):
    """Tests OTA Restore v3"""

    def setUp(self):
        self.domain = 'dummy-project'
        self.project = Domain(name=self.domain)
        self.project.save()
        delete_all_cases()
        delete_all_sync_logs()
        delete_all_users()

    def tearDown(self):
        self.project.delete()

    def testUserRestoreWithCase(self):
        restore_user = create_restore_user(domain=self.domain)
        case_id = 'my-case-id'
        device = MockDevice(self.project, restore_user)
        device.change_cases(CaseBlock(
            create=True,
            case_id=case_id,
            user_id=restore_user.user_id,
            owner_id=restore_user.user_id,
            case_type='test-case-type',
            update={'external_id': 'someexternal'},
        ))
        self.assertIn(case_id, device.sync().cases)


class TestRestoreResponse(SimpleTestCase):

    def _expected(self, username, body, items=None):
        items_text = ' items="{}"'.format(items) if items is not None else ''
        return (
            '<OpenRosaResponse xmlns="http://openrosa.org/http/response"{items}>'
            '<message nature="ota_restore_success">Successfully restored account {username}!</message>'
            '{body}'
            '</OpenRosaResponse>'
        ).format(
            username=username,
            body=body,
            items=items_text
        )

    def test_no_items(self):
        user = 'user1'
        body = '<elem>data0</elem>'
        expected = self._expected(user, body, items=None)
        with FileRestoreResponse(user, False) as response:
            response.append(body)
            response.finalize()
            self.assertEqual(expected, str(response))

    def test_items(self):
        user = 'user1'
        body = '<elem>data0</elem>'
        expected = self._expected(user, body, items=2)
        response = FileRestoreResponse(user, True)
        response.append(body)
        response.finalize()
        self.assertEqual(expected, str(response))

    def test_add(self):
        user = 'user1'
        body1 = '<elem>data0</elem>'
        body2 = '<elem>data1</elem>'
        expected = self._expected(user, body1 + body2, items=3)
        response1 = FileRestoreResponse(user, True)
        response1.append(body1)

        response2 = FileRestoreResponse(user, True)
        response2.append(body2)

        added = response1 + response2
        added.finalize()
        self.assertEqual(expected, str(added))

@@ -1557,16 +1557,18 @@
 _text = 
+(b
 ' items=
@@ -1572,20 +1572,15 @@
 ms=%22
-%7B%7D%22'.format(
+%25s%22' %25 
 item
@@ -1608,16 +1608,17 @@
 ne else 
+b
 ''%0A     
@@ -1641,16 +1641,17 @@
         
+b
 '%3COpenRo
@@ -1706,15 +1706,17 @@
 nse%22
-%7B
+%25(
 items
-%7D
+)s
 %3E'%0A 
@@ -1726,16 +1726,17 @@
         
+b
 '%3Cmessag
@@ -1800,17 +1800,18 @@
 unt 
-%7B
+%25(
 username
 %7D!%3C/
@@ -1806,17 +1806,18 @@
 username
-%7D
+)s
 !%3C/messa
@@ -1837,15 +1837,18 @@
     
-'%7B
+b'%25(
 body
-%7D
+)s
 '%0A  
@@ -1857,16 +1857,17 @@
         
+b
 '%3C/OpenR
@@ -1893,16 +1893,12 @@
    )
-.format(
+ %25 %7B
 %0A   
@@ -1906,24 +1906,26 @@
         
+b%22
 username
 =usernam
@@ -1920,17 +1920,19 @@
 name
-=
+%22: 
 username
 ,%0A  
@@ -1927,16 +1927,31 @@
 username
+.encode('utf8')
 ,%0A      
@@ -1960,13 +1960,17 @@
     
+b%22
 body
-=
+%22: 
 body
@@ -1983,22 +1983,26 @@
         
+b%22
 items
-=
+%22: 
 items_te
@@ -2003,16 +2003,17 @@
 ems_text
+,
 %0A       
@@ -2013,17 +2013,17 @@
         
-)
+%7D
 %0A%0A    de
@@ -2052,32 +2052,33 @@
 %0A        user = 
+u
 'user1'%0A        
@@ -2076,32 +2076,33 @@
 %0A        body = 
+b
 '%3Celem%3Edata0%3C/el
@@ -2261,44 +2261,8 @@
 dy)%0A
-            response.finalize()%0A    
     
@@ -2341,32 +2341,33 @@
 %0A        user = 
+u
 'user1'%0A        
@@ -2373,16 +2373,17 @@
  body = 
+b
 '%3Celem%3Ed
@@ -2462,18 +2462,12 @@
     
-response =
+with
  Fil
@@ -2498,534 +2498,54 @@
 rue)
-%0A        response.append(body)%0A        response.finalize()%0A        self.assertEqual(expected, str(response))%0A%0A    def test_add(self):%0A        user = 'user1'%0A        body1 = '%3Celem%3Edata0%3C/elem%3E'%0A        body2 = '%3Celem%3Edata1%3C/elem%3E'%0A        expected = self._expected(user, body1 + body2, items=3)%0A        response1 = FileRestoreResponse(user, True)%0A        response1.append(body1)%0A%0A        response2 = FileRestoreResponse(user, True)%0A        response2.append(body2)%0A%0A        added = response1 + response2%0A        added.finalize(
+ as response:%0A            response.append(body
 )%0A  
@@ -2585,12 +2585,15 @@
 str(
-added
+response
 ))%0A

Remove old test and fix python 3 compatibility