Exemplo n.º 1
0
// getResponseForCredentialsRequestWithParameters queries credentials for the
// given id. The getCredentials function is used to simulate getting the
// credentials object from the CredentialsManager
func getResponseForCredentialsRequestWithParameters(t *testing.T, expectedStatus int,
	expectedErrorMessage *errorMessage, id string, getCredentials func() (*credentials.TaskIAMRoleCredentials, bool)) (*bytes.Buffer, error) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	credentialsManager := mock_credentials.NewMockManager(ctrl)
	auditLog := mock_audit.NewMockAuditLogger(ctrl)
	server := setupServer(credentialsManager, auditLog)
	recorder := httptest.NewRecorder()

	creds, ok := getCredentials()
	credentialsManager.EXPECT().GetTaskCredentials(gomock.Any()).Return(creds, ok)
	auditLog.EXPECT().Log(gomock.Any(), gomock.Any(), gomock.Any())

	params := make(url.Values)
	params[credentials.CredentialsIdQueryParameterName] = []string{credentialsId}

	req, _ := http.NewRequest("GET", credentials.CredentialsPath+"?"+params.Encode(), nil)
	server.Handler.ServeHTTP(recorder, req)

	if recorder.Code != expectedStatus {
		return nil, fmt.Errorf("Expected return code: %d. Got: %d", expectedStatus, recorder.Code)
	}

	if recorder.Code != http.StatusOK {
		errorMessage := &errorMessage{}
		json.Unmarshal(recorder.Body.Bytes(), errorMessage)

		if errorMessage.Code != expectedErrorMessage.Code || errorMessage.Message != expectedErrorMessage.Message {
			return nil, fmt.Errorf("Unexpected values. Actual: %v| Expected: %v", errorMessage, expectedErrorMessage)
		}
	}

	return recorder.Body, nil
}
Exemplo n.º 2
0
func testErrorResponsesFromServer(t *testing.T, path string, expectedErrorMessage *errorMessage) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	credentialsManager := mock_credentials.NewMockManager(ctrl)
	auditLog := mock_audit.NewMockAuditLogger(ctrl)
	server := setupServer(credentialsManager, auditLog)

	recorder := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", path, nil)
	if path == credentials.CredentialsPath {
		auditLog.EXPECT().Log(gomock.Any(), gomock.Any(), gomock.Any())
	}

	server.Handler.ServeHTTP(recorder, req)
	httpErrorCode := http.StatusNotFound
	if expectedErrorMessage != nil {
		httpErrorCode = expectedErrorMessage.httpErrorCode
	}
	if recorder.Code != httpErrorCode {
		t.Fatalf("Expected return code: %d. Got: %d", httpErrorCode, recorder.Code)
	}

	// Only paths that are equal to /v1/credentials will return valid error responses.
	if path == credentials.CredentialsPath {
		errorMessage := &errorMessage{}
		json.Unmarshal(recorder.Body.Bytes(), errorMessage)
		if errorMessage.Code != expectedErrorMessage.Code ||
			errorMessage.Message != expectedErrorMessage.Message {
			t.Fatalf("Unexpected values. Actual: %v| Expected: %v", errorMessage, expectedErrorMessage)
		}
	}
}