working commit
This commit is contained in:
@@ -1,367 +0,0 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"mstore/app/handler"
|
||||
"mstore/app/operator"
|
||||
"mstore/app/router"
|
||||
"mstore/app/server"
|
||||
)
|
||||
|
||||
func TestAccountOperations(t *testing.T) {
|
||||
var err error
|
||||
fmt.Printf("=== MakeServer ===\n")
|
||||
srv, err := server.NewServer()
|
||||
require.NoError(t, err)
|
||||
|
||||
var srvport int64 = 10240 + rand.Int63n(1024)
|
||||
srvdir := t.TempDir()
|
||||
//srvaddr := fmt.Sprintf("127.0.0.1:%d", srvport)
|
||||
|
||||
{
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
|
||||
useTmpdir := true
|
||||
|
||||
if useTmpdir {
|
||||
srv.SetDatadir(srvdir)
|
||||
srv.SetLogdir(srvdir)
|
||||
srv.SetRundir(srvdir)
|
||||
}
|
||||
srv.SetPort(srvport)
|
||||
|
||||
err = srv.Build()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== ServiceHello ===\n")
|
||||
reqPath := "/service/hello"
|
||||
routePath := "/service/hello"
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
rout.Get(routePath, hand.SendHello)
|
||||
|
||||
request, err := http.NewRequest("GET", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
var accountID string
|
||||
var accountName = "testname1"
|
||||
{
|
||||
fmt.Printf("=== CreateAccount ===\n")
|
||||
reqpath := `/v3/api/account/create`
|
||||
routepath := `/v3/api/account/create`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.CreateAccountParams{
|
||||
Username: accountName,
|
||||
Password: "testpass",
|
||||
}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
rout.Post(routepath, hand.CreateAccount)
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.CreateAccountResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
require.Equal(t, len(resp.Result.AccountID), 36)
|
||||
accountID = resp.Result.AccountID
|
||||
}
|
||||
fmt.Printf("AccountID: %s\n", accountID)
|
||||
{
|
||||
fmt.Printf("=== UpdateAccount ===\n")
|
||||
reqpath := `/v3/api/account/create`
|
||||
routepath := `/v3/api/account/create`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.UpdateAccountParams{
|
||||
AccountID: accountID,
|
||||
NewPassword: "newpass",
|
||||
}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
rout.Post(routepath, hand.UpdateAccount)
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.UpdateAccountResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
}
|
||||
|
||||
{
|
||||
fmt.Printf("=== GetAccount ===\n")
|
||||
reqpath := `/v3/api/account/get`
|
||||
routepath := `/v3/api/account/get`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.GetAccountParams{
|
||||
AccountID: accountID,
|
||||
}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.Post(routepath, hand.GetAccount)
|
||||
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.GetAccountResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
require.Equal(t, resp.Result.Account.Username, accountName)
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== ListAccounts ===\n")
|
||||
reqpath := `/v3/api/accounts/list`
|
||||
routepath := `/v3/api/accounts/list`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.ListAccountsParams{}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.Post(routepath, hand.ListAccounts)
|
||||
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.ListAccountsResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
require.Equal(t, len(resp.Result.Accounts), 1)
|
||||
require.Equal(t, resp.Result.Accounts[0].Username, accountName)
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== DeleteAccount ===\n")
|
||||
reqpath := `/v3/api/account/delete`
|
||||
routepath := `/v3/api/account/delete`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.DeleteAccountParams{
|
||||
AccountID: accountID,
|
||||
}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.Post(routepath, hand.DeleteAccount)
|
||||
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.DeleteAccountResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== ListAccounts ===\n")
|
||||
reqpath := `/v3/api/accounts/list`
|
||||
routepath := `/v3/api/accounts/list`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
req := operator.ListAccountsParams{}
|
||||
reqdata, err := json.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
reqsize := len(reqdata)
|
||||
reqsrc := bytes.NewReader(reqdata)
|
||||
|
||||
request, err := http.NewRequest("POST", reqpath, reqsrc)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(reqsize)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.Post(routepath, hand.ListAccounts)
|
||||
|
||||
rout.ServeHTTP(recorder, request)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
jsonBuffer := bytes.NewBuffer(nil)
|
||||
err = json.Indent(jsonBuffer, bodyBytes, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("Formatted body: \n%s\n", jsonBuffer.String())
|
||||
|
||||
resp := handler.Response[operator.ListAccountsResult]{}
|
||||
err = json.Unmarshal(bodyBytes, &resp)
|
||||
require.False(t, resp.Error)
|
||||
require.Equal(t, len(resp.Result.Accounts), 0)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"mstore/app/router"
|
||||
"mstore/app/server"
|
||||
)
|
||||
|
||||
func xxxTestFileOperations(t *testing.T) {
|
||||
var err error
|
||||
fmt.Printf("=== MakeServer ===\n")
|
||||
srv, err := server.NewServer()
|
||||
require.NoError(t, err)
|
||||
|
||||
var srvport int64 = 10240 + rand.Int63n(1024)
|
||||
srvdir := t.TempDir()
|
||||
|
||||
filename := `bare.bin?abc=12`
|
||||
|
||||
{
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
var tmpdir bool
|
||||
tmpdir = true
|
||||
if tmpdir {
|
||||
srv.SetDatadir(srvdir)
|
||||
srv.SetLogdir(srvdir)
|
||||
srv.SetRundir(srvdir)
|
||||
}
|
||||
srv.SetPort(srvport)
|
||||
|
||||
err = srv.Build()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== ServiceHello ===\n")
|
||||
reqPath := "/service/hello"
|
||||
routePath := "/service/hello"
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
rout.Get(routePath, hand.SendHello)
|
||||
|
||||
request, err := http.NewRequest("GET", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== PutFile ===\n")
|
||||
reqPath := `/v3/api/file/` + filename
|
||||
routePath := `/v3/api/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Put(routePath, hand.PutFile)
|
||||
|
||||
datasize := 64
|
||||
filedata := make([]byte, datasize)
|
||||
_, err = rand.Read(filedata)
|
||||
require.NoError(t, err)
|
||||
|
||||
filedata = []byte(hex.EncodeToString(filedata))
|
||||
datasize *= 2
|
||||
|
||||
source := bytes.NewReader(filedata)
|
||||
|
||||
request, err := http.NewRequest("PUT", reqPath, source)
|
||||
require.NoError(t, err)
|
||||
|
||||
request.ContentLength = int64(datasize)
|
||||
request.Header.Set("Content-Type", "application/octet-stream")
|
||||
request.Header.Set("Content-Size", strconv.FormatInt(int64(datasize), 10))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== FileInfo ===\n")
|
||||
|
||||
reqPath := filepath.Join(`/v3/api/file`, filename)
|
||||
routePath := `/v3/api/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Head(routePath, hand.FileInfo)
|
||||
|
||||
request, err := http.NewRequest("HEAD", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== GetFile ===\n")
|
||||
reqPath := filepath.Join(`/v3/api/file`, filename)
|
||||
routePath := `/v3/api/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Get(routePath, hand.GetFile)
|
||||
|
||||
request, err := http.NewRequest("GET", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
{
|
||||
fmt.Printf("=== DeleteFile ===\n")
|
||||
reqPath := filepath.Join(`/v3/api/file`, filename)
|
||||
routePath := `/v3/api/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Delete(routePath, hand.DeleteFile)
|
||||
|
||||
request, err := http.NewRequest("DELETE", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
|
||||
{
|
||||
fmt.Printf("=== FileNotExists ===\n")
|
||||
|
||||
reqPath := filepath.Join(`/v3/api/file`, filename)
|
||||
routePath := `/v3/api/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Head(routePath, hand.FileInfo)
|
||||
|
||||
request, err := http.NewRequest("HEAD", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusNotFound, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user