def test_hours_service_post(self): mock_response = { "foo":"bar", } responses.add(responses.POST, "http://hoursservice.staging.tangentmicroservices.com/api/v1/entry/", body=json.dumps(mock_response), content_type="application/json", status=200) data = { "user": "******", "project_id": "43", "project_task_id": "57", "time": "October 1, 2015 at 09:34PM", "entered_or_exited": "exited", "auth_token" : "abcdef123456" } result = IfThisThenThatHelpers.make_hours_post(data, 8) from requests import Response assert isinstance(result, Response) assert result.json() == mock_response assert result is not None, "Result Empty"
def test_zero_time_diff(self): enter = "April 29, 2013 at 12:01PM" exit = "April 29, 2013 at 12:01PM" result = IfThisThenThatHelpers.calculate_hours_diff(enter, exit) assert result == 0, 'Equals 0 hours'
def test_over_day_time_diff(self): enter = "April 23, 2013 at 12:01PM" exit = "April 24, 2013 at 12:01PM" result = IfThisThenThatHelpers.calculate_hours_diff(enter, exit) assert result == 24, 'More than 24 hours. result = {}' . format (result)
def test_negative_time_diff(self): enter = "April 29, 2013 at 09:01PM" exit = "April 29, 2013 at 08:01PM" result = IfThisThenThatHelpers.calculate_hours_diff(enter, exit) assert result < 0, 'Less than 0 hours'
def test_post_to_hipchat(self, mock_get_current_user, mock_get_project, mock_hipchat_speak): mock_user_response = MockResponse({"first_name": "Joe"}) mock_project_response = MockResponse({"title": "ACME"}) mock_get_current_user.return_value = mock_user_response mock_get_project.return_value = mock_project_response payload = { 'auth_token': '123', 'user': 1, 'project_id': 2, 'entered_or_exited': 'entered' } # item under test: IfThisThenThatHelpers.post_to_hipchat(payload) # assertions mock_hipchat_speak.assert_called_with('Joe has arrived at ACME')
def test_calculate_difference(self): inputs = [ ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:01PM", 0), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:15PM", 0), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:16PM", 0.5), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:30PM", 0.5), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:31PM", 0.5), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:45PM", 0.5), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 08:46PM", 1), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 09:01PM", 1), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 09:15PM", 1), ("April 29, 2013 at 08:01PM", "April 29, 2013 at 09:16PM", 1.5) ] for entered, exited, expected in inputs: result = IfThisThenThatHelpers.calculate_hours_diff(entered, exited) assert result == expected, 'Expect {} to be {})' .format (result, expected)
def create(self, request): data = request.data #Save Record icr = IncomingRequest() icr.payload = json.dumps(data) # store payload as string #Get the User if 'user' in request.data: icr.user = data['user'] icr.source = 'IT' icr.incoming_url = request.META.get('HTTP_REFFERER') icr.save() if not data or data is None: return Response({'message': 'ERROR', 'description': 'No data is set'}, status=400) if data.get('entered_or_exited', None) == 'entered': IfThisThenThatHelpers.post_to_hipchat(icr.payload_as_json) # If exiting an area find corresponding entry time if data.get('entered_or_exited', None) == "exited": #if IncomingRequest.objects.filter(user=icr.user).order_by('-id')[1].exists(): try: entered_icr = IncomingRequest.objects.filter(user=icr.user).order_by('-id')[1] except IndexError: data = {'message': 'ERROR', 'description': 'could not find a matching enter entry'} return Response(data, status=200, content_type='application/json') # get the total number of hours entered_data = entered_icr.payload_as_json exited_data = icr.payload_as_json hours = IfThisThenThatHelpers.get_hours(entered_data, exited_data) if hours > 0 and hours <= 24: token = entered_data["auth_token"] # Make the Hours Request response = IfThisThenThatHelpers.make_hours_post(entered_data, hours) IfThisThenThatHelpers.post_to_hipchat(icr.payload_as_json) hipchat_speak("{} hours logged" . format (hours)) else: message = "Invalid hours amount encountered: {} hours. Please capture your hours manually" . format (hours) hipchat_speak(message) # Check the response from hours return Response({'message': 'OK', 'data': data}, status=200)