from __future__ import unicode_literals

import json
from nose.tools import assert_equal
from hamcrest import assert_that

from django.test import TestCase
from django_nose.tools import assert_redirects

from stagecraft.apps.datasets.tests.support.test_helpers import (
    is_unauthorized, is_error_response, has_header, has_status)


class LongCacheTestCase(TestCase):
    def test_detail_sets_long_cache_headers(self):
        resp = self.client.get(
            '/data-sets/set1',
            HTTP_AUTHORIZATION='Bearer dev-data-set-query-token')
        assert_that(resp, has_header('Cache-Control', 'max-age=31536000'))
        assert_that(resp, has_header('Vary', 'Authorization'))


class BackdropUserViewsTestCase(TestCase):
    fixtures = ['backdrop_users_testdata.json']

    # def test_authorization_header_needed_for_detail(self):
    #     resp = self.client.get('/users/tea%40yourmumshouse.com')
    #     assert_that(resp, is_unauthorized())
    #     assert_that(resp, is_error_response())

    # def test_correct_format_authorization_header_needed_for_detail(self):
    #     resp = self.client.get(
    #         '/data-sets/set1',
    #         HTTP_AUTHORIZATION='Nearer dev-data-set-query-token')
    #     assert_that(resp, is_unauthorized())
    #     assert_that(resp, is_error_response())

    # def test_correct_authorization_header_needed_for_detail(self):
    #     resp = self.client.get(
    #         '/data-sets/set1',
    #         HTTP_AUTHORIZATION='Bearer I AM WRONG')
    #     assert_that(resp, is_unauthorized())
    #     assert_that(resp, is_error_response())

    def test_detail(self):
        resp = self.client.get(
            '/users/foo%40bar.com',
            HTTP_AUTHORIZATION='Bearer dev-data-set-query-token')
        assert_equal(resp.status_code, 200)
        expected = {'data_sets': ['set1', 'set2'], 'email': 'foo@bar.com'}
        assert_equal(json.loads(resp.content.decode('utf-8')), expected)

    def test_detail_nonexistant_user(self):
        resp = self.client.get(
            '/users/nonexistant-user',
            HTTP_AUTHORIZATION='Bearer dev-data-set-query-token')
        assert_equal(resp.status_code, 404)

@@ -775,26 +775,24 @@
 .json'%5D%0A%0A   
- #
  def test_au
@@ -830,34 +830,32 @@
 etail(self):%0A   
- #
      resp = self
@@ -899,26 +899,24 @@
 se.com')%0A   
- #
      assert_
@@ -940,34 +940,32 @@
 uthorized())%0A   
- #
      assert_that
@@ -988,34 +988,32 @@
 response())%0A%0A   
- #
  def test_correc
@@ -1062,34 +1062,32 @@
 etail(self):%0A   
- #
      resp = self
@@ -1099,33 +1099,32 @@
 nt.get(%0A    
-#
         
  '/data-sets
@@ -1103,33 +1103,32 @@
 et(%0A            
- 
 '/data-sets/set1
@@ -1125,34 +1125,32 @@
 -sets/set1',%0A   
- #
          HTTP_AU
@@ -1195,26 +1195,24 @@
 -token')%0A   
- #
      assert_
@@ -1236,34 +1236,32 @@
 uthorized())%0A   
- #
      assert_that
@@ -1292,18 +1292,16 @@
 ())%0A%0A   
- #
  def tes
@@ -1355,26 +1355,24 @@
 l(self):%0A   
- #
      resp = 
@@ -1387,26 +1387,24 @@
 ent.get(%0A   
- #
          '/d
@@ -1418,26 +1418,24 @@
 s/set1',%0A   
- #
          HTT
@@ -1470,26 +1470,24 @@
  WRONG')%0A   
- #
      assert_
@@ -1523,10 +1523,8 @@
 %0A   
- #
     

Add in common authorization methods to backdrop user view tests