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)
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')