<commit_before>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase, Client
from auth.models import Account
from cloudfish.models import Cloud
from cloudfish import CLOUD_RACKSPACE
from django.core.signing import BadSignature
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User


class KeyEncriptiontest(TestCase):
    def test_encode_decode_auth_data(self):
        """
        Tests that the auth_data is correctly signed
        """
        account = Account(username='daltonmatos', password='<PASSWORD>')
        cloud = Cloud(type=CLOUD_RACKSPACE, account=account)

        auth_data = {'user': 'username', 'key': 'my_api_key'}

        _data = cloud.add_auth_data(salt='mypassword', **auth_data)
        self.assertEquals(_data, cloud.auth_data)
        self.assertRaises(BadSignature, cloud.decode_auth_data, salt='worng-salt')
        self.assertEquals(auth_data, cloud.decode_auth_data(salt='mypassword'))


class PrepareSessionTest(TestCase):
    def test_put_auth_data_in_session(self):
        user = Account.objects.create_user(username='<EMAIL>', password='<PASSWORD>')
        user.save()

        auth_data = {'user': 'awsuser', 'key': 'mykey'}

        aws = Cloud(type="AM", account=user)
        aws.add_auth_data(salt='pass', **auth_data)
        aws.save()

        self.assertEquals(1, len(Cloud.objects.filter(account=user)))
        self.assertEquals(auth_data, Cloud.objects.filter(account=user)[0].decode_auth_data(salt='pass'))

        client = Client()
        self.assertTrue(client.login(username='<EMAIL>', password='<PASSWORD>'))
        self.assertEquals(auth_data, client.session['clouds']['AM'])


class EmailValidationTest(TestCase):
    def test_unique_email(self):
        account = Account(email="<EMAIL>", password="<PASSWORD>")
        account.save()
        client = Client()
        response = client.post("/register", {"email": "<EMAIL>", "password": "<PASSWORD>", "confirm-password": "<PASSWORD>"})

        self.assertIn("This email is already in use.", response.content)


class ConnectOnFirstLoginTest(TestCase):
    def test_redirect_to_connect_if_no_clouds_found(self):
        """
        If the user dows not have any connected cloud backend, redirect to the
        connect view
        """
        user = User.objects.create_user(username='newuser', password='<PASSWORD>')
        user.save()

        client = Client()
        response = client.post("/auth/login", {'username': 'newuser', 'password': '<PASSWORD>'})
        self.assertIn(reverse('connect-view'), response['Location'])


class ConnectAccountTest(TestCase):
    def test_connect_account(self):
        user = Account.objects.create_user(username="newuser", password="<PASSWORD>")
        user.save()

        client = Client()
        client.post("/auth/login", {'username': 'newuser', 'password': '<PASSWORD>'})
        client.post("/connect", {"aws_key_id": 1, "aws_secret_key": "my_secrete_key"})

        self.assertEqual(1, Cloud.objects.filter(account=user).count())
<commit_msg>Green test<commit_after>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase, Client
from auth.models import Account
from cloudfish.models import Cloud
from cloudfish import CLOUD_RACKSPACE
from django.core.signing import BadSignature
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User


class KeyEncriptiontest(TestCase):
    def test_encode_decode_auth_data(self):
        """
        Tests that the auth_data is correctly signed
        """
        account = Account(username='daltonmatos', password='<PASSWORD>')
        cloud = Cloud(type=CLOUD_RACKSPACE, account=account)

        auth_data = {'user': 'username', 'key': 'my_api_key'}

        _data = cloud.add_auth_data(salt='mypassword', **auth_data)
        self.assertEquals(_data, cloud.auth_data)
        self.assertRaises(BadSignature, cloud.decode_auth_data, salt='worng-salt')
        self.assertEquals(auth_data, cloud.decode_auth_data(salt='mypassword'))


class PrepareSessionTest(TestCase):
    def test_put_auth_data_in_session(self):
        user = Account.objects.create_user(username='<EMAIL>', password='<PASSWORD>')
        user.save()

        auth_data = {'user': 'awsuser', 'key': 'mykey'}

        aws = Cloud(type="AM", account=user)
        aws.add_auth_data(salt='pass', **auth_data)
        aws.save()

        self.assertEquals(1, len(Cloud.objects.filter(account=user)))
        self.assertEquals(auth_data, Cloud.objects.filter(account=user)[0].decode_auth_data(salt='pass'))

        client = Client()
        client.post("/auth/login", {"username": '<EMAIL>', "password": "<PASSWORD>"})
        self.assertEquals(auth_data, client.session['clouds']['AM'])


class EmailValidationTest(TestCase):
    def test_unique_email(self):
        account = Account(email="<EMAIL>", password="<PASSWORD>")
        account.save()
        client = Client()
        response = client.post("/register", {"email": "<EMAIL>", "password": "<PASSWORD>", "confirm-password": "<PASSWORD>"})

        self.assertIn("This email is already in use.", response.content)


class ConnectOnFirstLoginTest(TestCase):
    def test_redirect_to_connect_if_no_clouds_found(self):
        """
        If the user dows not have any connected cloud backend, redirect to the
        connect view
        """
        user = User.objects.create_user(username='newuser', password='<PASSWORD>')
        user.save()

        client = Client()
        response = client.post("/auth/login", {'username': 'newuser', 'password': '<PASSWORD>'})
        self.assertIn(reverse('connect-view'), response['Location'])


class ConnectAccountTest(TestCase):
    def test_connect_account(self):
        user = Account.objects.create_user(username="newuser", password="<PASSWORD>")
        user.save()

        client = Client()
        client.post("/auth/login", {'username': 'newuser', 'password': '<PASSWORD>'})
        client.post("/connect", {"aws_key_id": 1, "aws_secret_key": "my_secrete_key"})

        self.assertEqual(1, Cloud.objects.filter(account=user).count())

{'fraction_non_alphanumeric': 0.13902743142144638, 'fraction_numerical': 0.0012468827930174563, 'mean_word_length': 3.8910060975609757, 'pattern_counts': {'":': 12, '<': 31, '<?xml version=': 0, '>': 31, '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': '4863046', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1820, 'n_words': 398}