from appengine.images import main
import mock
from tests import DatastoreTestbedCase

import webapp2


class TestHandlers(DatastoreTestbedCase):
    def test_get(self):
        # Build a request object passing the URI path to be tested.
        # You can also pass headers, query arguments etc.
        request = webapp2.Request.blank('/')
        # Get a response for that request.
        response = request.get_response(main.app)

        # Let's check if the response is correct.
        self.assertEqual(response.status_int, 200)

    @mock.patch('appengine.images.main.images')
    def test_post(self, mock_images):
        mock_images.resize.return_value = 'asdf'
        request = webapp2.Request.blank(
            '/sign',
            POST={'content': 'asdf'},
            )
        response = request.get_response(main.app)
        mock_images.resize.assert_called_once_with(mock.ANY, 32, 32)

        # Correct response is a redirect
        self.assertEqual(response.status_int, 302)

    def test_img(self):
        greeting = main.Greeting(
            parent=main.guestbook_key('default_guestbook'),
            id=123,
        )
        greeting.author = 'asdf'
        greeting.content = 'asdf'
        greeting.put()

        request = webapp2.Request.blank(
            '/img?img_id=%s' % greeting.key.urlsafe()
        )
        response = request.get_response(main.app)

        self.assertEqual(response.status_int, 200)

    def test_img_missing(self):
        # Bogus image id, should get error
        request = webapp2.Request.blank('/img?img_id=123')
        response = request.get_response(main.app)

        self.assertEqual(response.status_int, 500)

    @mock.patch('appengine.images.main.images')
    def test_post_and_get(self, mock_images):
        mock_images.resize.return_value = 'asdf'
        request = webapp2.Request.blank(
            '/sign',
            POST={'content': 'asdf'},
            )
        response = request.get_response(main.app)

        request = webapp2.Request.blank('/')
        response = request.get_response(main.app)

        self.assertEqual(response.status_int, 200)
