# -*- coding: utf-8 -*-


from django.test import TestCase
from django.core import mail
from django.core.urlresolvers import reverse
from django.utils import simplejson as json

from django.contrib.auth.models import User
from ..models import *

from greenmine import permissions as perms
from django.utils import timezone
import datetime

from greenmine.utils import mail as mailutils


class LowLevelEmailTests(TestCase):
    def setUp(self):
        self.user = User.objects.create(
            username = 'test',
            email = 'test@test.com',
            is_active = True,
            is_staff = False,
            is_superuser = False,
        )
        self.user.set_password('test')
        self.user.save()
        mail.outbox = []

    def test_send_recovery_mail(self):
        mailutils.send_recovery_email(self.user)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Greenmine: password recovery.")

    def test_send_registration_mail(self):
        mailutils.send_new_registration_mail(self.user)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Greenmine: Welcome!")


@@ -1172,12 +1172,1684 @@
 Welcome!%22)%0A%0A
+class UserMailTests(TestCase):%0A    def setUp(self):%0A        self.user1 = User.objects.create(%0A            username = 'test1',%0A            email = 'test1@test.com',%0A            is_active = True,%0A            is_staff = True,%0A            is_superuser = True,%0A        )%0A%0A        self.user2 = User.objects.create(%0A            username = 'test2',%0A            email = 'test2@test.com',%0A            is_active = True,%0A            is_staff = False,%0A            is_superuser = False,%0A        )%0A%0A        self.user1.set_password(%22test%22)%0A        self.user2.set_password(%22test%22)%0A%0A        self.user1.save()%0A        self.user2.save()%0A%0A        mail.outbox = %5B%5D%0A%0A    def test_send_recovery_password_by_staff(self):%0A        url = reverse(%22web:users-recovery-password%22, args=%5Bself.user2.pk%5D)%0A        %0A        ok = self.client.login(username=%22test1%22, password=%22test%22)%0A        self.assertTrue(ok)%0A        %0A        # pre test%0A        self.assertTrue(self.user2.is_active)%0A        self.assertEqual(self.user2.get_profile().token, None)%0A%0A        response = self.client.get(url, follow=True)%0A        self.assertEqual(response.status_code, 200)%0A        %0A        # expected redirect%0A        self.assertEqual(response.redirect_chain, %5B('http://testserver/users/2/edit/', 302)%5D)%0A%0A        # test mail sending%0A        self.assertEqual(len(mail.outbox), 1)%0A        self.assertEqual(mail.outbox%5B0%5D.subject, %22Greenmine: password recovery.%22)%0A%0A        # test user model modification%0A        self.user2 = User.objects.get(pk=self.user2.pk)%0A        self.assertTrue(self.user2.is_active)%0A        self.assertFalse(self.user2.has_usable_password())%0A        self.assertNotEqual(self.user2.get_profile().token, None)%0A

Work in progress