Migrate tests from "api4/apitestlib.go" to use testify (#12817)

* migrate tests to use testify

* check resp error

* require empty auth

* fix based on comment

* change to require

* change all assert to require

* remove line
Этот коммит содержится в:
Agus Mistiawan
2019-10-27 18:54:19 +07:00
коммит произвёл Miguel de la Cruz
родитель 4d962aaade
Коммит 3701a393e5

Просмотреть файл

@@ -5,13 +5,10 @@ package api4
import ( import (
"fmt" "fmt"
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
"reflect"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@@ -27,6 +24,7 @@ import (
s3 "github.com/minio/minio-go/v6" s3 "github.com/minio/minio-go/v6"
"github.com/minio/minio-go/v6/pkg/credentials" "github.com/minio/minio-go/v6/pkg/credentials"
"github.com/stretchr/testify/require"
) )
type TestHelper struct { type TestHelper struct {
@@ -561,57 +559,34 @@ func GenerateTestId() string {
func CheckUserSanitization(t *testing.T, user *model.User) { func CheckUserSanitization(t *testing.T, user *model.User) {
t.Helper() t.Helper()
if user.Password != "" { require.Equal(t, "", user.Password, "password wasn't blank")
t.Fatal("password wasn't blank") require.Empty(t, user.AuthData, "auth data wasn't blank")
} require.Equal(t, "", user.MfaSecret, "mfa secret wasn't blank")
if user.AuthData != nil && *user.AuthData != "" {
t.Fatal("auth data wasn't blank")
}
if user.MfaSecret != "" {
t.Fatal("mfa secret wasn't blank")
}
} }
func CheckEtag(t *testing.T, data interface{}, resp *model.Response) { func CheckEtag(t *testing.T, data interface{}, resp *model.Response) {
t.Helper() t.Helper()
if !reflect.ValueOf(data).IsNil() { require.Empty(t, data)
t.Fatal("etag data was not nil") require.Equal(t, resp.StatusCode, http.StatusNotModified, "wrong status code for etag")
}
if resp.StatusCode != http.StatusNotModified {
t.Log("actual: " + strconv.Itoa(resp.StatusCode))
t.Log("expected: " + strconv.Itoa(http.StatusNotModified))
t.Fatal("wrong status code for etag")
}
} }
func CheckNoError(t *testing.T, resp *model.Response) { func CheckNoError(t *testing.T, resp *model.Response) {
t.Helper() t.Helper()
if resp.Error != nil { require.Nil(t, resp.Error)
t.Fatalf("Expected no error, got %q", resp.Error.Error())
}
} }
func checkHTTPStatus(t *testing.T, resp *model.Response, expectedStatus int, expectError bool) { func checkHTTPStatus(t *testing.T, resp *model.Response, expectedStatus int, expectError bool) {
t.Helper() t.Helper()
switch { require.NotNilf(t, resp, "Unexpected nil response, expected http:%v, expectError:%v", expectedStatus, expectError)
case resp == nil: if expectError {
t.Fatalf("Unexpected nil response, expected http:%v, expectError:%v)", expectedStatus, expectError) require.NotNil(t, resp.Error, "Expected a non-nil error and http status:%v, got nil, %v", expectedStatus, resp.StatusCode)
} else {
case expectError && resp.Error == nil: require.Nil(t, resp.Error, "Expected no error and http status:%v, got %q, http:%v", expectedStatus, resp.Error, resp.StatusCode)
t.Fatalf("Expected a non-nil error and http status:%v, got nil, %v", expectedStatus, resp.StatusCode)
case !expectError && resp.Error != nil:
t.Fatalf("Expected no error and http status:%v, got %q, http:%v", expectedStatus, resp.Error, resp.StatusCode)
case resp.StatusCode != expectedStatus:
t.Fatalf("Expected http status:%v, got %v (err: %q)", expectedStatus, resp.StatusCode, resp.Error)
} }
require.Equalf(t, expectedStatus, resp.StatusCode, "Expected http status:%v, got %v (err: %q)", expectedStatus, resp.StatusCode, resp.Error)
} }
func CheckOKStatus(t *testing.T, resp *model.Response) { func CheckOKStatus(t *testing.T, resp *model.Response) {
@@ -662,16 +637,8 @@ func CheckInternalErrorStatus(t *testing.T, resp *model.Response) {
func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) { func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) {
t.Helper() t.Helper()
if resp.Error == nil { require.NotNil(t, resp.Error)
t.Fatal("should have errored with message:" + errorId) require.Equal(t, resp.Error.Id, errorId, "incorrect error message")
return
}
if resp.Error.Id != errorId {
t.Log("actual: " + resp.Error.Id)
t.Log("expected: " + errorId)
t.Fatal("incorrect error message")
}
} }
func CheckStartsWith(t *testing.T, value, prefix, message string) { func CheckStartsWith(t *testing.T, value, prefix, message string) {