mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-05-01 20:10:41 -05:00
Added tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# Gokapi
|
||||
[](https://gokapi.readthedocs.io/en/stable/?badge=stable)
|
||||
[](https://goreportcard.com/report/github.com/forceu/gokapi)
|
||||
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'></a>
|
||||
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'></a>
|
||||
[](https://hub.docker.com/r/f0rc3/gokapi/)
|
||||
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/forceu/gokapi/internal/test/testconfiguration"
|
||||
"github.com/forceu/gokapi/internal/webserver/authentication"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -622,3 +623,26 @@ func createInputOAuth() setupValues {
|
||||
values.PicturesAlwaysLocal.Value = "local"
|
||||
return values
|
||||
}
|
||||
|
||||
func TestIsErrorAddressAlreadyInUse(t *testing.T) {
|
||||
|
||||
l, err := net.Listen("tcp", "127.0.0.1:19888")
|
||||
test.IsNil(t, err)
|
||||
srv2 := http.Server{
|
||||
Addr: ":19888",
|
||||
}
|
||||
httpError := make(chan error)
|
||||
go func() {
|
||||
sErr := srv2.ListenAndServe()
|
||||
httpError <- sErr
|
||||
}()
|
||||
select {
|
||||
case err = <-httpError:
|
||||
test.IsEqualBool(t, isErrorAddressAlreadyInUse(err), true)
|
||||
case <-time.After(15 * time.Second):
|
||||
t.Fatalf("15 seconds timeout")
|
||||
}
|
||||
err = errors.New("other error")
|
||||
test.IsEqualBool(t, isErrorAddressAlreadyInUse(err), false)
|
||||
l.Close()
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"github.com/forceu/gokapi/internal/models"
|
||||
"github.com/forceu/gokapi/internal/test"
|
||||
"github.com/forceu/gokapi/internal/test/testconfiguration"
|
||||
"io/ioutil"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -45,7 +44,7 @@ func TestAddString(t *testing.T) {
|
||||
// Need sleep, as AddString() is non-blocking
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
test.FileExists(t, "test/log.txt")
|
||||
content, _ := ioutil.ReadFile("test/log.txt")
|
||||
content, _ := os.ReadFile("test/log.txt")
|
||||
test.IsEqualBool(t, strings.Contains(string(content), "UTC Hello"), true)
|
||||
}
|
||||
|
||||
@@ -60,7 +59,11 @@ func TestAddDownload(t *testing.T) {
|
||||
AddDownload(&file, r)
|
||||
// Need sleep, as AddDownload() is non-blocking
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
content, _ := ioutil.ReadFile("test/log.txt")
|
||||
content, _ := os.ReadFile("test/log.txt")
|
||||
fmt.Println(string(content))
|
||||
test.IsEqualBool(t, strings.Contains(string(content), "UTC Download: Filename testName, IP 1.1.1.1, ID testId, Useragent testAgent"), true)
|
||||
}
|
||||
|
||||
func TestGetLogPath(t *testing.T) {
|
||||
test.IsEqualString(t, GetLogPath(), "test/log.txt")
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func (d *s3StorageDriver) MoveToFilesystem(sourceFile *os.File, metaData models.
|
||||
func (d *s3StorageDriver) Init(input any) bool {
|
||||
config, ok := input.(Config)
|
||||
if !ok {
|
||||
panic("runtime exception: input for local filesystem is not a config object")
|
||||
panic("runtime exception: input for aws filesystem is not a config object")
|
||||
}
|
||||
if config.Bucket == "" {
|
||||
panic("empty bucket has been passed")
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package s3filesystem
|
||||
|
||||
import (
|
||||
"github.com/forceu/gokapi/internal/test"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getTestDriver(t *testing.T) *s3StorageDriver {
|
||||
t.Helper()
|
||||
driver := GetDriver()
|
||||
result, ok := driver.(*s3StorageDriver)
|
||||
test.IsEqualBool(t, ok, true)
|
||||
return result
|
||||
}
|
||||
|
||||
func TestGetDriver(t *testing.T) {
|
||||
getTestDriver(t)
|
||||
}
|
||||
|
||||
func TestS3StorageDriver_Init(t *testing.T) {
|
||||
driver := getTestDriver(t)
|
||||
defer test.ExpectPanic(t)
|
||||
driver.Init("test")
|
||||
defer test.ExpectPanic(t)
|
||||
driver.Init(Config{Bucket: ""})
|
||||
}
|
||||
func TestS3StorageDriver_Init2(t *testing.T) {
|
||||
driver := getTestDriver(t)
|
||||
defer test.ExpectPanic(t)
|
||||
driver.Init(Config{Bucket: ""})
|
||||
}
|
||||
|
||||
func TestS3StorageDriver_Init3(t *testing.T) {
|
||||
driver := getTestDriver(t)
|
||||
ok := driver.Init(Config{Bucket: "test"})
|
||||
test.IsEqualBool(t, ok, false)
|
||||
test.IsEqualString(t, driver.Bucket, "test")
|
||||
test.IsEqualBool(t, driver.IsAvailable(), false) // TODO
|
||||
}
|
||||
|
||||
func TestS3StorageDriver_GetSystemName(t *testing.T) {
|
||||
driver := getTestDriver(t)
|
||||
test.IsEqualString(t, driver.GetSystemName(), "awss3")
|
||||
}
|
||||
|
||||
func TestAwsFile_GetName(t *testing.T) {
|
||||
driver := getTestDriver(t)
|
||||
driver.Init(Config{Bucket: "test"})
|
||||
file := driver.GetFile("testfile")
|
||||
test.IsEqualString(t, file.GetName(), "testfile")
|
||||
}
|
||||
@@ -47,6 +47,10 @@ func (t *MockTest) Check() {
|
||||
}
|
||||
}
|
||||
|
||||
type testStruct struct {
|
||||
Value1 int64
|
||||
}
|
||||
|
||||
func TestFunctions(t *testing.T) {
|
||||
mockT := MockTest{reference: t}
|
||||
mockT.WantNoFail()
|
||||
@@ -70,9 +74,13 @@ func TestFunctions(t *testing.T) {
|
||||
os.WriteFile("testfile", []byte("content"), 0777)
|
||||
mockT.WantNoFail()
|
||||
FileExists(mockT, "testfile")
|
||||
mockT.WantNoFail()
|
||||
IsEqualStruct(mockT, testStruct{Value1: 1337}, testStruct{Value1: 1337})
|
||||
|
||||
mockT.WantNoFail()
|
||||
IsNotNil(mockT, errors.New("hello"))
|
||||
mockT.WantNoFail()
|
||||
IsNotNil(mockT, "test")
|
||||
mockT.WantFail()
|
||||
IsEqualString(mockT, "test", "test2")
|
||||
mockT.WantFail()
|
||||
@@ -90,6 +98,8 @@ func TestFunctions(t *testing.T) {
|
||||
mockT.WantFail()
|
||||
IsNil(mockT, errors.New("hello"))
|
||||
mockT.WantFail()
|
||||
IsNil(mockT, "test")
|
||||
mockT.WantFail()
|
||||
IsNilWithMessage(mockT, errors.New("hello"), "there")
|
||||
mockT.WantFail()
|
||||
IsNotNil(mockT, nil)
|
||||
@@ -100,6 +110,8 @@ func TestFunctions(t *testing.T) {
|
||||
os.Remove("testfile")
|
||||
mockT.WantFail()
|
||||
FileExists(mockT, "testfile")
|
||||
mockT.WantFail()
|
||||
IsEqualStruct(mockT, testStruct{Value1: 1337}, testStruct{Value1: 1338})
|
||||
mockT.Check()
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ func TestEnableS3(t *testing.T) {
|
||||
test.IsEqualString(t, os.Getenv("GOKAPI_AWS_REGION"), "mock-region-1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisableS3S3(t *testing.T) {
|
||||
DisableS3()
|
||||
if aws.IsMockApi {
|
||||
@@ -45,6 +46,15 @@ func TestDisableS3S3(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseMockS3Server(t *testing.T) {
|
||||
previousValue := os.Getenv("REAL_AWS_CREDENTIALS")
|
||||
os.Setenv("REAL_AWS_CREDENTIALS", "false")
|
||||
test.IsEqualBool(t, UseMockS3Server(), true)
|
||||
os.Setenv("REAL_AWS_CREDENTIALS", "true")
|
||||
test.IsEqualBool(t, UseMockS3Server(), false)
|
||||
os.Setenv("REAL_AWS_CREDENTIALS", previousValue)
|
||||
}
|
||||
|
||||
func TestWriteSslCertificates(t *testing.T) {
|
||||
test.FileDoesNotExist(t, "test/ssl.key")
|
||||
WriteSslCertificates(true)
|
||||
@@ -66,3 +76,8 @@ func TestWriteCloudConfigFile(t *testing.T) {
|
||||
test.FileExists(t, "test/cloudconfig.yml")
|
||||
Delete()
|
||||
}
|
||||
|
||||
func TestStartS3TestServer(t *testing.T) {
|
||||
server := StartS3TestServer()
|
||||
test.IsNotNil(t, server)
|
||||
}
|
||||
|
||||
@@ -508,3 +508,10 @@ func TestList(t *testing.T) {
|
||||
test.IsEqualInt(t, w.Code, 200)
|
||||
test.ResponseBodyContains(t, w, "picture.jpg")
|
||||
}
|
||||
|
||||
func TestApiRequestToUploadRequest(t *testing.T) {
|
||||
_, r := test.GetRecorder("POST", "/api/chunk/complete", nil, []test.Header{
|
||||
{Name: "Content-type", Value: "application/x-www-form-urlencoded"}}, strings.NewReader("invalid&&§$%"))
|
||||
_, _, _, err := apiRequestToUploadRequest(r)
|
||||
test.IsNotNil(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user