#  -*- coding: utf-8 -*-
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import unittest

from mock import patch
from mockito import mock, when, verify, any as ANY
from twisted.internet import defer
from twisted.web.test.requesthelper import DummyRequest

from pixelated.application import UserAgentMode
from pixelated.resources.mails_resource import MailsResource
from test.unit.resources import DummySite


class TestMailsResource(unittest.TestCase):
    def setUp(self):
        self.mail_service = mock()
        self.services_factory = mock()
        self.services_factory.mode = UserAgentMode(is_single_user=True)
        self.services = mock()
        self.services.mail_service = self.mail_service
        self.services.draft_service = mock()
        self.services_factory._services_by_user = {'someuserid': self.mail_service}
        when(self.services_factory).services(ANY()).thenReturn(self.services)

    @patch('leap.common.events.register')
    def test_render_GET_should_unicode_mails_search_query(self, mock_register):
        request = DummyRequest(['/mails'])
        non_unicode_search_term = 'coração'
        request.addArg('q', non_unicode_search_term)
        request.addArg('w', 25)
        request.addArg('p', 1)

        unicodified_search_term = u'coração'
        when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.Deferred())

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).mails(unicodified_search_term, 25, 1)

        d.addCallback(assert_response)
        return d

    @patch('leap.common.events.register')
    def test_render_PUT_should_store_draft_with_attachments(self, mock_register):
        request = DummyRequest(['/mails'])
        request.method = 'PUT'
        content = mock()
        when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}')
        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred())
        request.content = content

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).attachment('some fake attachment id')

        d.addCallback(assert_response)
        return d

    @patch('leap.common.events.register')
    def test_render_POST_should_send_email_with_attachments(self, mock_register):
        request = DummyRequest(['/mails'])
        request.method = 'POST'
        content = mock()
        when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}')
        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"}))
        as_dictable = mock()
        when(as_dictable).as_dict().thenReturn({})
        when(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})\
            .thenReturn(defer.succeed(as_dictable))
        request.content = content

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).attachment('some fake attachment id')
            verify(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})

        d.addCallback(assert_response)
        return d

@@ -707,16 +707,35 @@
 nses/%3E.%0A
+from twisted.trial 
 import u
@@ -1717,32 +1717,24 @@
 mmyRequest(%5B
-'/mails'
 %5D)%0A        n
@@ -2017,25 +2017,31 @@
 n(defer.
-Deferred(
+succeed((%5B%5D, 0)
 ))%0A%0A    
@@ -2102,45 +2102,8 @@
 ry)%0A
-        mails_resource.isLeaf = True%0A
     
@@ -2486,32 +2486,24 @@
 mmyRequest(%5B
-'/mails'
 %5D)%0A        r
@@ -2524,32 +2524,40 @@
 = 'PUT'%0A        
+request.
 content = mock()
@@ -2562,32 +2562,40 @@
 ()%0A        when(
+request.
 content).read().
@@ -2755,53 +2755,37 @@
 fer.
-Deferred())%0A        request.content = content
+succeed(%7B'content': mock()%7D))
 %0A%0A  
@@ -2848,45 +2848,8 @@
 ry)%0A
-        mails_resource.isLeaf = True%0A
     
@@ -3240,16 +3240,8 @@
 st(%5B
-'/mails'
 %5D)%0A 
@@ -3279,16 +3279,24 @@
         
+request.
 content 
@@ -3317,16 +3317,24 @@
    when(
+request.
 content)
@@ -3794,42 +3794,8 @@
 le))
-%0A        request.content = content
 %0A%0A  
@@ -3858,45 +3858,8 @@
 ry)%0A
-        mails_resource.isLeaf = True%0A
     

fix mails resource unit test