|
@@ -1 +1,199 @@
|
|
package anonym_list
|
|
package anonym_list
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/url"
|
|
|
|
+ "os"
|
|
|
|
+ "strings"
|
|
|
|
+ "testing"
|
|
|
|
+
|
|
|
|
+ "github.com/gofiber/fiber/v2"
|
|
|
|
+
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_service"
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_store"
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_web"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type tester struct {
|
|
|
|
+ t *testing.T
|
|
|
|
+ serv *mock_service.MockService
|
|
|
|
+ web *mock_web.MockWeb
|
|
|
|
+ router fiber.Router
|
|
|
|
+ anonList *AnonymList
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestAnonymList(t *testing.T) {
|
|
|
|
+ fnClear := func() {
|
|
|
|
+ _ = os.RemoveAll("./store")
|
|
|
|
+ }
|
|
|
|
+ fnClear()
|
|
|
|
+ defer fnClear()
|
|
|
|
+ sf := &tester{
|
|
|
|
+ t: t,
|
|
|
|
+ }
|
|
|
|
+ sf.create()
|
|
|
|
+ sf.get()
|
|
|
|
+ sf.post()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ПОСТ-запрос для получения списка ключей
|
|
|
|
+func (sf *tester) post() {
|
|
|
|
+ sf.t.Log("post")
|
|
|
|
+ sf.postBad1()
|
|
|
|
+ sf.postBad2()
|
|
|
|
+ sf.postGood1()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sf *tester) postGood1() {
|
|
|
|
+ sf.t.Log("postGood1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/list", sf.anonList.post)
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {"key"},
|
|
|
|
+ }
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/list", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ if err := sf.serv.Store_.Put("key", []byte("val")); err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ err = sf.serv.Store_.Put("anon/key2", []byte("val2"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ if resp.StatusCode != 200 {
|
|
|
|
+ sf.t.Fatalf("postGood1(): resp.Status=%v", resp.Status)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Ошибка поиска ключей в хранилище
|
|
|
|
+func (sf *tester) postBad2() {
|
|
|
|
+ sf.t.Log("postBad2")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/list", sf.anonList.post)
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {"key"},
|
|
|
|
+ }
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/list", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ mock_store.IsBad_ = true
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ mock_store.IsBad_ = false
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Кривой запрос на сервер
|
|
|
|
+func (sf *tester) postBad1() {
|
|
|
|
+ sf.t.Log("postBad1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/list", sf.anonList.post)
|
|
|
|
+ formReq := url.Values{}
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/list", strings.NewReader(formReq.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ // req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ // Здесь проблема с расположением шаблонов в другом месте
|
|
|
|
+ // if resp.StatusCode != 200 {
|
|
|
|
+ // sf.t.Fatalf("postBad1(): resp.Status=%v", resp.Status)
|
|
|
|
+ // }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Получение страницы списка ключей
|
|
|
|
+func (sf *tester) get() {
|
|
|
|
+ sf.t.Log("get")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Get("/anonym/list", sf.anonList.get)
|
|
|
|
+ req, err := http.NewRequest("GET", "/anonym/list", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("get(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("get(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ // Здесь проблема с расположением шаблонов в другом месте
|
|
|
|
+ // if resp.StatusCode != 200 {
|
|
|
|
+ // sf.t.Fatalf("get(): resp.Status=%v", resp.Status)
|
|
|
|
+ // }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Создаёт новый обработчик списка анонимных ключей
|
|
|
|
+func (sf *tester) create() {
|
|
|
|
+ sf.t.Log("create")
|
|
|
|
+ sf.createBad1()
|
|
|
|
+ sf.createBad2()
|
|
|
|
+ sf.createGood1()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sf *tester) createGood1() {
|
|
|
|
+ sf.t.Log("createGood1")
|
|
|
|
+ var err error
|
|
|
|
+ sf.anonList, err = NewAnonymList(sf.web, sf.router)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ if sf.anonList == nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): anonList==nil")
|
|
|
|
+ }
|
|
|
|
+ if serv := sf.anonList.serv; serv == nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): serv==nil")
|
|
|
|
+ }
|
|
|
|
+ if store := sf.anonList.store; store == nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): store==nil")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Нет объекта роутера
|
|
|
|
+func (sf *tester) createBad2() {
|
|
|
|
+ sf.t.Log("createBad2")
|
|
|
|
+ anonList, err := NewAnonymList(sf.web, nil)
|
|
|
|
+ if err == nil {
|
|
|
|
+ sf.t.Fatalf("createBad2(): err==nil")
|
|
|
|
+ }
|
|
|
|
+ if anonList != nil {
|
|
|
|
+ sf.t.Fatalf("createBad2(): anonList!=nil")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Нет объекта веб-сервера
|
|
|
|
+func (sf *tester) createBad1() {
|
|
|
|
+ sf.t.Log("createBad1")
|
|
|
|
+ var err error
|
|
|
|
+ sf.serv = mock_service.NewMockService()
|
|
|
|
+ sf.serv.Store_, err = mock_store.NewMockStore(sf.serv)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ sf.web, err = mock_web.NewMockWeb(sf.serv)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ sf.router = sf.web.FiberApp().Group("/anonym")
|
|
|
|
+ anonList, err := NewAnonymList(nil, nil)
|
|
|
|
+ if err == nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err==nil")
|
|
|
|
+ }
|
|
|
|
+ if anonList != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): anonList!=nil")
|
|
|
|
+ }
|
|
|
|
+}
|