# coding=utf-8

from __future__ import unicode_literals

from datetime import date, timedelta
import os

from django.conf import settings
from django.core.files.base import ContentFile
from django.test import TestCase

from zivinetz.models import Assignment, AssignmentChange

from testapp.tests import factories


class ZivinetzTestCase(TestCase):
    def test_create_profile_as_drudge(self):
        # Hit a few views, just for fun
        self.assertRedirects(
            self.client.get('/zivinetz/'),
            'http://testserver/accounts/login/?next=/zivinetz/')
        self.assertRedirects(
            self.client.get('/zivinetz/dashboard/'),
            'http://testserver/accounts/login/?next=/zivinetz/dashboard/')

        user = factories.UserFactory.create()
        self.client.login(username=user.username, password='test')

        self.assertRedirects(
            self.client.get('/zivinetz/'),
            'http://testserver/zivinetz/profile/')

        response = self.client.post('/zivinetz/profile/', {
            'first_name': 'Hans',
            'last_name': 'Muster',
            'zdp_no': '12345',
            'address': 'Musterstrasse 42',
            'zip_code': '8000',
            'city': 'Zürich',
            'date_of_birth': '1980-01-01',
            'place_of_citizenship_city': 'Zürich',
            'place_of_citizenship_state': 'ZH',
            'bank_account': 'CH77-12345',
            'environment_course': '2',  # Yes.
            'regional_office': factories.RegionalOfficeFactory.create().id,
        })
        self.assertRedirects(
            response,
            'http://testserver/zivinetz/profile/')

        self.assertRedirects(
            self.client.get('/zivinetz/dashboard/'),
            'http://testserver/zivinetz/profile/')
        self.assertRedirects(
            self.client.get('/zivinetz/dashboard/'),
            'http://testserver/zivinetz/profile/')

        path = os.path.join(
            os.path.dirname(settings.BASE_DIR),
            'zivinetz',
            'data',
            '3-0.jpg')

        with open(path) as image, ContentFile(image.read()) as cf:
            user.drudge.profile_image.save('profile.jpg', cf)

        response = self.client.get('/zivinetz/dashboard/')
        self.assertEqual(response.status_code, 200)

        self.assertRedirects(
            self.client.get('/zivinetz/'),
            'http://testserver/zivinetz/dashboard/')

    def test_create_assignment_as_drudge(self):
        drudge = factories.DrudgeFactory.create()
        self.client.login(username=drudge.user.username, password='test')

        data = {
            'assignment': '1',
            'specification': factories.SpecificationFactory.create().id,
            'regional_office': drudge.regional_office.id,
            'date_from': date.today(),
            'date_until': date.today() + timedelta(days=60),
            'part_of_long_assignment': '',
            'codeword': 'whatever',
        }

        response = self.client.post('/zivinetz/dashboard/', data)
        self.assertContains(response, 'Codeword is incorrect.')

        factories.CodewordFactory.create(key='einsatz', codeword='blaaa')
        data['codeword'] = 'blaaa'

        response = self.client.post('/zivinetz/dashboard/', data)
        self.assertRedirects(
            response,
            'http://testserver/zivinetz/dashboard/')

        assignment = Assignment.objects.get()
        self.assertEqual(assignment.date_from, date.today())
        self.assertEqual(assignment.status, Assignment.TENTATIVE)
        self.assertEqual(AssignmentChange.objects.count(), 1)

@@ -268,16 +268,26 @@
 ntChange
+, WaitList
 %0A%0Afrom t
@@ -3633,8 +3633,855 @@
 t(), 1)%0A
+%0A    def test_create_waitlist_as_drudge(self):%0A        drudge = factories.DrudgeFactory.create()%0A        self.client.login(username=drudge.user.username, password='test')%0A%0A        factories.CodewordFactory.create(key='warteliste', codeword='blaeu')%0A%0A        data = %7B%0A            'waitlist': '1',%0A            'specification': factories.SpecificationFactory.create().id,%0A            'assignment_date_from': date.today(),%0A            'assignment_date_until': date.today() + timedelta(days=60),%0A            'assignment_duration': 42,%0A            'codeword': 'blaeu',%0A        %7D%0A%0A        response = self.client.post('/zivinetz/dashboard/', data)%0A        self.assertRedirects(%0A            response,%0A            'http://testserver/zivinetz/dashboard/')%0A%0A        waitlist = WaitList.objects.get()%0A        self.assertEqual(waitlist.assignment_duration, 42)%0A

Add a test for adding waitlist entries as drudge