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
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
4d962aaade
Коммит
3701a393e5
@@ -5,13 +5,10 @@ package api4
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -27,6 +24,7 @@ import (
|
||||
|
||||
s3 "github.com/minio/minio-go/v6"
|
||||
"github.com/minio/minio-go/v6/pkg/credentials"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type TestHelper struct {
|
||||
@@ -561,57 +559,34 @@ func GenerateTestId() string {
|
||||
func CheckUserSanitization(t *testing.T, user *model.User) {
|
||||
t.Helper()
|
||||
|
||||
if user.Password != "" {
|
||||
t.Fatal("password 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")
|
||||
}
|
||||
require.Equal(t, "", user.Password, "password wasn't blank")
|
||||
require.Empty(t, user.AuthData, "auth data wasn't blank")
|
||||
require.Equal(t, "", user.MfaSecret, "mfa secret wasn't blank")
|
||||
}
|
||||
|
||||
func CheckEtag(t *testing.T, data interface{}, resp *model.Response) {
|
||||
t.Helper()
|
||||
|
||||
if !reflect.ValueOf(data).IsNil() {
|
||||
t.Fatal("etag data was not nil")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
require.Empty(t, data)
|
||||
require.Equal(t, resp.StatusCode, http.StatusNotModified, "wrong status code for etag")
|
||||
}
|
||||
|
||||
func CheckNoError(t *testing.T, resp *model.Response) {
|
||||
t.Helper()
|
||||
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("Expected no error, got %q", resp.Error.Error())
|
||||
}
|
||||
require.Nil(t, resp.Error)
|
||||
}
|
||||
|
||||
func checkHTTPStatus(t *testing.T, resp *model.Response, expectedStatus int, expectError bool) {
|
||||
t.Helper()
|
||||
|
||||
switch {
|
||||
case resp == nil:
|
||||
t.Fatalf("Unexpected nil response, expected http:%v, expectError:%v)", expectedStatus, expectError)
|
||||
|
||||
case expectError && resp.Error == nil:
|
||||
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.NotNilf(t, resp, "Unexpected nil response, expected http:%v, expectError:%v", expectedStatus, expectError)
|
||||
if expectError {
|
||||
require.NotNil(t, resp.Error, "Expected a non-nil error and http status:%v, got nil, %v", expectedStatus, resp.StatusCode)
|
||||
} else {
|
||||
require.Nil(t, resp.Error, "Expected no error and http status:%v, got %q, http:%v", expectedStatus, resp.Error, resp.StatusCode)
|
||||
}
|
||||
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) {
|
||||
@@ -662,16 +637,8 @@ func CheckInternalErrorStatus(t *testing.T, resp *model.Response) {
|
||||
func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) {
|
||||
t.Helper()
|
||||
|
||||
if resp.Error == nil {
|
||||
t.Fatal("should have errored with message:" + errorId)
|
||||
return
|
||||
}
|
||||
|
||||
if resp.Error.Id != errorId {
|
||||
t.Log("actual: " + resp.Error.Id)
|
||||
t.Log("expected: " + errorId)
|
||||
t.Fatal("incorrect error message")
|
||||
}
|
||||
require.NotNil(t, resp.Error)
|
||||
require.Equal(t, resp.Error.Id, errorId, "incorrect error message")
|
||||
}
|
||||
|
||||
func CheckStartsWith(t *testing.T, value, prefix, message string) {
|
||||
|
||||
Ссылка в новой задаче
Block a user