import mock

from datetime import datetime
from flask import current_app
from oauth2client import GOOGLE_REVOKE_URI, GOOGLE_TOKEN_URI
from oauth2client.client import OAuth2Credentials

from changes.models import User
from changes.testutils import TestCase


class LoginViewTest(TestCase):
    def test_simple(self):
        resp = self.client.get('/auth/login/')
        assert resp.status_code == 302
        assert resp.headers['Location'] == \
            'https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=http%3A%2F%2Flocalhost%2Fauth%2Fcomplete%2F&response_type=code&client_id=aaaaaaaaaaaa&access_type=offline'


class AuthorizedViewTest(TestCase):
    @mock.patch('changes.web.auth.OAuth2WebServerFlow.step2_exchange')
    def test_simple(self, step2_exchange):
        access_token = 'b' * 40
        refresh_token = 'c' * 40

        step2_exchange.return_value = OAuth2Credentials(
            access_token, current_app.config['GOOGLE_CLIENT_ID'],
            current_app.config['GOOGLE_CLIENT_SECRET'],
            refresh_token,
            datetime(2013, 9, 19, 22, 15, 22),
            GOOGLE_TOKEN_URI,
            'foo/1.0',
            revoke_uri=GOOGLE_REVOKE_URI,
            id_token={
                'hd': 'example.com',
                'email': 'foo@example.com',
            },
        )

        resp = self.client.get('/auth/complete/?code=abc')

        step2_exchange.assert_called_once_with('abc')

        assert resp.status_code == 302
        assert resp.headers['Location'] == 'http://localhost/'

        user = User.query.filter(
            User.email == 'foo@example.com',
        ).first()

        assert user


class LogoutViewTest(TestCase):
    def test_simple(self):
        resp = self.client.get('/auth/logout/')
        assert resp.status_code == 302
        assert resp.headers['Location'] == 'http://localhost/'

@@ -176,16 +176,56 @@
 dentials
+%0Afrom urlparse import urlparse, parse_qs
 %0A%0Afrom c
@@ -314,32 +314,33 @@
 Test(TestCase):%0A
+%0A
     def test_sim
@@ -436,39 +436,59 @@
  == 302%0A        
-assert 
+parsed_location = urlparse(
 resp.headers%5B'Lo
@@ -499,13 +499,9 @@
 on'%5D
- == %5C
+)
 %0A   
@@ -509,75 +509,251 @@
     
-    'https://accounts.google.com/o/oauth2/auth?scope=https%253A%252F%252F
+assert parsed_location.scheme == 'https'%0A        assert parsed_location.netloc == 'accounts.google.com'%0A        assert parsed_location.path == '/o/oauth2/auth'%0A        assert parse_qs(parsed_location.query) == %7B%0A            'scope': %5B'https://
 www.
@@ -766,26 +766,22 @@
 apis.com
-%252F
+/
 auth
-%252F
+/
 userinfo
@@ -790,35 +790,49 @@
 mail
-&redirect_uri=http%253A%252F%252F
+'%5D,%0A            'redirect_uri': %5B'http://
 loca
@@ -840,26 +840,22 @@
 host
-%252F
+/
 auth
-%252F
+/
 complete
 %252F&
@@ -854,31 +854,65 @@
 lete
-%252F&response_type=code&
+/'%5D,%0A            'response_type': %5B'code'%5D,%0A            '
 clie
@@ -916,17 +916,21 @@
 lient_id
-=
+': %5B'
 aaaaaaaa
@@ -937,9 +937,25 @@
 aaaa
-&
+'%5D,%0A            '
 acce
@@ -965,17 +965,21 @@
 type
-=
+': %5B'
 offline'
 %0A%0A%0Ac
@@ -974,16 +974,27 @@
 offline'
+%5D%0A        %7D
 %0A%0A%0Aclass
@@ -1020,24 +1020,25 @@
 (TestCase):%0A
+%0A
     @mock.pa
@@ -1898,16 +1898,39 @@
 calhost/
+?finished_login=success
 '%0A%0A     
@@ -2044,16 +2044,16 @@
  user%0A%0A%0A
-
 class Lo
@@ -2072,24 +2072,25 @@
 (TestCase):%0A
+%0A
     def test

Fix unit tests