<s> import datetime <EOL> import json <EOL> import pytz <EOL> from django . http import HttpRequest , QueryDict , Http404 <EOL> from django . test import Client , TestCase <EOL> from mock import patch <EOL> from rest_framework . request import Request <EOL> from rest_framework . authtoken . models import Token <EOL> from periods import models as period_models , views <EOL> from periods . serializers import FlowEventSerializer <EOL> from periods . tests . factories import FlowEventFactory , UserFactory <EOL> class TestFlowEventViewSet ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . view_set = views . FlowEventViewSet ( ) <EOL> self . view_set . format_kwarg = '<STR_LIT>' <EOL> self . period = FlowEventFactory ( ) <EOL> FlowEventFactory ( timestamp = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) ) ) <EOL> self . request = Request ( HttpRequest ( ) ) <EOL> self . request . __setattr__ ( '<STR_LIT:user>' , self . period . user ) <EOL> self . view_set . request = self . request <EOL> def test_list ( self ) : <EOL> response = self . view_set . list ( self . request ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( response . data ) ) <EOL> self . assertEqual ( self . period . id , response . data [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] ) <EOL> def test_perform_create ( self ) : <EOL> serializer = FlowEventSerializer ( data = { '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) } ) <EOL> serializer . is_valid ( ) <EOL> self . view_set . perform_create ( serializer ) <EOL> self . assertEqual ( self . request . user , serializer . instance . user ) <EOL> class TestStatisticsViewSet ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . view_set = views . StatisticsViewSet ( ) <EOL> self . view_set . format_kwarg = '<STR_LIT>' <EOL> self . period = FlowEventFactory ( ) <EOL> self . period2 = FlowEventFactory ( timestamp = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) ) ) <EOL> self . request = Request ( HttpRequest ( ) ) <EOL> self . request . __setattr__ ( '<STR_LIT:user>' , self . period . user ) <EOL> self . view_set . request = self . request <EOL> def test_retrieve_other_user ( self ) : <EOL> self . view_set . kwargs = { '<STR_LIT>' : self . period2 . user . statistics . pk } <EOL> try : <EOL> self . view_set . retrieve ( self . request ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except Http404 : <EOL> pass <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_list_no_params ( self , mock_today ) : <EOL> mock_today . return_value = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:5> ) ) <EOL> self . view_set . kwargs = { '<STR_LIT>' : self . request . user . statistics . pk } <EOL> response = self . view_set . list ( self . request ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( response . data ) ) <EOL> self . assertEqual ( <NUM_LIT> , response . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . period . timestamp . date ( ) , response . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , response . data [ '<STR_LIT>' ] ) <EOL> def test_list_with_min_timestamp ( self ) : <EOL> http_request = HttpRequest ( ) <EOL> http_request . GET = QueryDict ( u'<STR_LIT>' ) <EOL> request = Request ( http_request ) <EOL> request . __setattr__ ( '<STR_LIT:user>' , self . period . user ) <EOL> self . view_set . kwargs = { '<STR_LIT>' : self . request . user . statistics . pk } <EOL> response = self . view_set . list ( request ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( response . data ) ) <EOL> self . assertEqual ( <NUM_LIT> , response . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . period . timestamp . date ( ) , response . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , response . data [ '<STR_LIT>' ] ) <EOL> class TestApiAuthenticate ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . client = Client ( ) <EOL> self . url_path = '<STR_LIT>' <EOL> self . data = { "<STR_LIT:email>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } <EOL> def test_api_authenticate_get ( self ) : <EOL> response = self . client . get ( self . url_path ) <EOL> self . assertEqual ( <NUM_LIT> , response . status_code ) <EOL> def test_api_authenticate_not_json ( self ) : <EOL> response = self . client . post ( self . url_path , data = '<STR_LIT>' , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( <NUM_LIT> , response . status_code ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> def test_api_authenticate_missing_fields ( self ) : <EOL> response = self . client . post ( self . url_path , data = json . dumps ( { } ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( <NUM_LIT> , response . status_code ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> def test_api_authenticate_failure ( self ) : <EOL> response = self . client . post ( self . url_path , data = json . dumps ( self . data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( <NUM_LIT> , response . status_code ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_api_authenticate_success ( self , mock_authenticate ) : <EOL> user = UserFactory ( ) <EOL> mock_authenticate . return_value = user <EOL> response = self . client . post ( self . url_path , data = json . dumps ( self . data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertContains ( response , user . auth_token . key ) <EOL> class TestViews ( TestCase ) : <EOL> maxDiff = None <EOL> def setUp ( self ) : <EOL> self . period = FlowEventFactory ( ) <EOL> FlowEventFactory ( user = self . period . user , <EOL> timestamp = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) ) ) <EOL> self . request = HttpRequest ( ) <EOL> self . request . user = self . period . user <EOL> self . request . META [ '<STR_LIT>' ] = '<STR_LIT:localhost>' <EOL> self . request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_period_form_no_parameters ( self , mock_today ) : <EOL> mock_today . return_value = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:7> ) ) <EOL> response = views . period_form ( self . request , mock_today ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_period_form_with_timestamp ( self ) : <EOL> self . request . GET = QueryDict ( '<STR_LIT>' ) <EOL> response = views . period_form ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_period_form_invalid_period_id ( self , mock_today ) : <EOL> mock_today . return_value = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:7> ) ) <EOL> response = views . period_form ( self . request , <NUM_LIT> ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_period_form_existing_period ( self ) : <EOL> response = views . period_form ( self . request , self . period . id ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_calendar ( self ) : <EOL> response = views . calendar ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_cycle_length_frequency_no_data ( self ) : <EOL> period_models . FlowEvent . objects . all ( ) . delete ( ) <EOL> response = views . cycle_length_frequency ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( { } , result ) <EOL> def test_cycle_length_frequency ( self ) : <EOL> response = views . cycle_length_frequency ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( { '<STR_LIT>' : [ [ <NUM_LIT> , <NUM_LIT:1> ] ] } , result ) <EOL> def test_cycle_length_history_no_data ( self ) : <EOL> period_models . FlowEvent . objects . all ( ) . delete ( ) <EOL> response = views . cycle_length_history ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( { } , result ) <EOL> def test_cycle_length_history ( self ) : <EOL> response = views . cycle_length_history ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( { '<STR_LIT>' : [ [ '<STR_LIT>' , <NUM_LIT> ] ] } , result ) <EOL> def test_qigong_cycles_no_birth_date ( self ) : <EOL> self . request . user . birth_date = None <EOL> self . request . user . save ( ) <EOL> response = views . qigong_cycles ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( { } , result ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_qigong_cycles ( self , mock_today ) : <EOL> mock_today . return_value = pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:20> ) ) <EOL> response = views . qigong_cycles ( self . request ) <EOL> result = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> expected = { <EOL> '<STR_LIT>' : [ [ '<STR_LIT>' , <NUM_LIT:0> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:100> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:0> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT> ] ] , <EOL> '<STR_LIT>' : [ [ '<STR_LIT>' , <NUM_LIT:0> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:100> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:0> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT> ] ] , <EOL> '<STR_LIT>' : [ [ '<STR_LIT>' , <NUM_LIT:0> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:100> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT> ] , <EOL> [ '<STR_LIT>' , <NUM_LIT:0> ] ] , <EOL> } <EOL> self . assertEqual ( expected , result ) <EOL> def test_statistics_no_data ( self ) : <EOL> period_models . FlowEvent . objects . all ( ) . delete ( ) <EOL> response = views . statistics ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_statistics ( self ) : <EOL> response = views . statistics ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_profile_post_invalid_data ( self ) : <EOL> self . request . method = '<STR_LIT:POST>' <EOL> self . request . POST = QueryDict ( u'<STR_LIT>' ) <EOL> response = views . profile ( self . request ) <EOL> user = period_models . User . objects . get ( pk = self . request . user . pk ) <EOL> self . assertEqual ( pytz . utc . localize ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) , user . birth_date ) <EOL> self . assertContains ( response , '<STR_LIT>' % self . request . user . email ) <EOL> def test_profile_post_valid_data ( self ) : <EOL> self . request . method = '<STR_LIT:POST>' <EOL> self . request . POST = QueryDict ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> response = views . profile ( self . request ) <EOL> user = period_models . User . objects . get ( pk = self . request . user . pk ) <EOL> self . assertEqual ( u'<STR_LIT>' , user . first_name ) <EOL> self . assertEqual ( <NUM_LIT:12> , user . luteal_phase_length ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_profile_get ( self ) : <EOL> response = views . profile ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_api_info ( self ) : <EOL> response = views . api_info ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_regenerate_key_post ( self ) : <EOL> self . request . method = '<STR_LIT:POST>' <EOL> api_key = Token . objects . get ( user = self . request . user ) . key <EOL> response = views . regenerate_key ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' , status_code = <NUM_LIT> ) <EOL> self . assertNotEquals ( api_key , self . request . user . auth_token . key ) <EOL> def test_regenerate_key_get ( self ) : <EOL> api_key = Token . objects . get ( user = self . request . user ) . key <EOL> response = views . regenerate_key ( self . request ) <EOL> self . assertContains ( response , '<STR_LIT>' , status_code = <NUM_LIT> ) <EOL> self . assertEquals ( api_key , self . request . user . auth_token . key ) </s>
