from __future__ import absolute_import
from __future__ import unicode_literals

import json

from django.test import SimpleTestCase
from mock import patch, Mock
from corehq.motech.requests import Requests


TEST_API_URL = 'http://localhost:9080/api/'
TEST_API_USERNAME = 'admin'
TEST_API_PASSWORD = 'district'
TEST_DOMAIN = 'test-domain'


class RequestsTests(SimpleTestCase):

    def setUp(self):
        self.requests = Requests(TEST_DOMAIN, TEST_API_URL, TEST_API_USERNAME, TEST_API_PASSWORD)
        self.org_unit_id = 'abc'
        self.data_element_id = '123'

    def test_authentication(self):
        with patch('corehq.motech.requests.RequestLog', Mock()), \
                patch('corehq.motech.requests.requests') as requests_mock:
            content = {'code': TEST_API_USERNAME}
            content_json = json.dumps(content)
            response_mock = Mock()
            response_mock.status_code = 200
            response_mock.content = content_json
            response_mock.json.return_value = content
            requests_mock.get.return_value = response_mock

            response = self.requests.get('me')
            requests_mock.get.assert_called_with(
                TEST_API_URL + 'me',
                headers={'Accept': 'application/json'},
                auth=(TEST_API_USERNAME, TEST_API_PASSWORD)
            )
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.json()['code'], TEST_API_USERNAME)

    def test_send_data_value_set(self):
        with patch('corehq.motech.requests.RequestLog', Mock()), \
                patch('corehq.motech.requests.requests') as requests_mock:
            payload = {'dataValues': [
                {'dataElement': self.data_element_id, 'period': "201701",
                 'orgUnit': self.org_unit_id, 'value': "180"},
                {'dataElement': self.data_element_id, 'period': "201702",
                 'orgUnit': self.org_unit_id, 'value': "200"},
            ]}
            content = {'status': 'SUCCESS', 'importCount': {'imported': 2}}
            content_json = json.dumps(content)
            response_mock = Mock()
            response_mock.status_code = 201
            response_mock.content = content_json
            response_mock.json.return_value = content
            requests_mock.post.return_value = response_mock

            response = self.requests.post('dataValueSets', json=payload)
            requests_mock.post.assert_called_with(
                'http://localhost:9080/api/dataValueSets',
                json=payload,
                headers={'Content-type': 'application/json', 'Accept': 'application/json'},
                auth=(TEST_API_USERNAME, TEST_API_PASSWORD)
            )
            self.assertEqual(response.status_code, 201)
            self.assertEqual(response.json()['status'], 'SUCCESS')
            self.assertEqual(response.json()['importCount']['imported'], 2)

    def test_verify_ssl(self):
        with patch('corehq.motech.requests.RequestLog', Mock()), \
                patch('corehq.motech.requests.requests') as requests_mock:

            requests = Requests(TEST_DOMAIN, TEST_API_URL, TEST_API_USERNAME, TEST_API_PASSWORD, verify=False)
            requests.get('me')
            requests_mock.get.assert_called_with(
                TEST_API_URL + 'me',
                headers={'Accept': 'application/json'},
                auth=(TEST_API_USERNAME, TEST_API_PASSWORD),
                verify=False
            )

@@ -35,31 +35,9 @@
 port
-%0Afrom __future__ import
+,
  uni
@@ -102,16 +102,33 @@
 TestCase
+%0A%0Aimport requests
 %0Afrom mo
@@ -141,19 +141,20 @@
 ort 
-patch, Mock
+Mock, patch%0A
 %0Afro
@@ -684,41 +684,42 @@
 atch
-('corehq.motech.requests.
+.object(requests.Session, '
 request
-s
 ') a
@@ -719,33 +719,32 @@
 est') as request
-s
 _mock:%0A         
@@ -1032,27 +1032,22 @@
  request
-s
 _mock.
-get.
 return_v
@@ -1130,35 +1130,30 @@
      request
-s
 _mock.
-get.
 assert_calle
@@ -1176,35 +1176,96 @@
         
-TEST_API_URL + 'me'
+'GET',%0A                TEST_API_URL + 'me',%0A                allow_redirects=True
 ,%0A      
@@ -1650,41 +1650,42 @@
 atch
-('corehq.motech.requests.
+.object(requests.Session, '
 request
-s
 ') a
@@ -1689,25 +1689,24 @@
 ) as request
-s
 _mock:%0A     
@@ -2352,28 +2352,22 @@
  request
-s
 _mock.
-post.
 return_v
@@ -2484,20 +2484,14 @@
 uest
-s
 _mock.
-post.
 asse
@@ -2498,32 +2498,56 @@
 rt_called_with(%0A
+                'POST',%0A
                 
@@ -2589,16 +2589,43 @@
 eSets',%0A
+                data=None,%0A
         
@@ -3135,41 +3135,42 @@
 atch
-('corehq.motech.requests.
+.object(requests.Session, '
 request
-s
 ') a
@@ -3174,25 +3174,24 @@
 ) as request
-s
 _mock:%0A%0A    
@@ -3190,32 +3190,37 @@
 k:%0A%0A            
+self.
 requests = Reque
@@ -3306,32 +3306,37 @@
 se)%0A            
+self.
 requests.get('me
@@ -3361,19 +3361,14 @@
 uest
-s
 _mock.
-get.
 asse
@@ -3399,35 +3399,96 @@
         
-TEST_API_URL + 'me'
+'GET',%0A                TEST_API_URL + 'me',%0A                allow_redirects=True
 ,%0A      

Update tests