<commit_before>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': '<EMAIL>'}
        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)
<commit_msg>Add in common authorization methods to backdrop user view tests<commit_after>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': '<EMAIL>'}
        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)

{'fraction_non_alphanumeric': 0.11156278229448961, 'fraction_numerical': 0.01084010840108401, 'mean_word_length': 3.437875751503006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6200151', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1376, 'n_words': 221}