from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

#from __future__ import print_function
from .statements import *

nu = datetime.datetime.now()

#@pytest.mark.django_db
@override_settings(TESTING=True)
class NewUserRegistrationTest(TestCase):
    def setUp(self):
        self.c = Client()
        redis_connection.publish('tasks','KILL')

    def tearDown(self):
        try:
            os.remove('rowsandall_workouts_2018-01-01_2019-01-01.csv')
        except:
            pass

    @patch('rowers.dataprep.workout_summary_to_df',side_effect=mock_workout_summaries)
    def test_newuser(self,mock_workout_summaries):
        form_data = {
            'first_name':'Jan',
            'last_name':'Roeiert',
            'email':'jan@loop.nl',
            'username':'janderoeiert',
            'password1':'aapindewei2',
            'password2':'aapindewei2',
            'tos':True,
            'weightcategory':'hwt',
            'adaptiveclass': 'None',
            'sex':'male',
            'next':'/rowers/list-workouts',
            'birthdate':datetime.datetime(year=1970,month=4,day=2)
            }

        form = RegistrationFormUniqueEmail(form_data)
        self.assertTrue(form.is_valid())

        response = self.c.post('/rowers/register/', form_data, follow=True)

        self.assertRedirects(response,
                             expected_url='/rowers/me/gdpr-optin/?next=/rowers/list-workouts/',
                             status_code=302,target_status_code=200)


        url = '/rowers/me/gdpr-optin-confirm/?next=/rowers/list-workouts/'

        response = self.c.get(url)

        self.assertRedirects(response,
                             expected_url='/rowers/list-workouts/',
                             status_code=302,target_status_code=200,
                             )

        url = '/rowers/exportallworkouts/'

        response = self.c.get(url,follow=True)


        self.assertTrue(response.status_code,200)


        form_data = {
            'startdate':'2018-01-01',
            'enddate':'2019-01-01'
        }

        response = self.c.post(url,form_data)

        self.assertTrue(response.status_code,200)

        url = '/rowers/me/delete/'

        form_data = {
            'delete_user':True,
            }

        response = self.c.post(url,form_data,follow=True)
        self.assertRedirects(response,
                            expected_url='/login/',
                            status_code=302,target_status_code=200)
