Browse Source

SVI Добавление тестов; 65.1%

SVI 10 months ago
parent
commit
6a3e49f8d1
1 changed files with 272 additions and 0 deletions
  1. 272 0
      internal/serv_http/router_anonym/anonym_read/anoneym_read_test.go

+ 272 - 0
internal/serv_http/router_anonym/anonym_read/anoneym_read_test.go

@@ -1 +1,273 @@
 package anonym_read
+
+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
+	store    *mock_store.MockStore
+	web      *mock_web.MockWeb
+	router   fiber.Router
+	anonRead *AnonymRead
+}
+
+func TestAnonymRead(t *testing.T) {
+	fnClear := func() {
+		_ = os.RemoveAll("./store")
+	}
+	fnClear()
+	defer fnClear()
+	sf := &tester{
+		t: t,
+	}
+	sf.create()
+	sf.read()
+	sf.get()
+	sf.readCheck()
+}
+
+// Онлайн проверка ключа на получение
+func (sf *tester) readCheck() {
+	sf.t.Log("readCheck")
+	sf.readCheckBad1()
+	sf.readCheckBad2()
+	sf.readCheckGood1()
+}
+
+func (sf *tester) readCheckGood1() {
+	sf.t.Log("readCheckGood1")
+	app := fiber.New()
+	app.Post("/anonym/get_check_key", sf.anonRead.readCheckKey)
+	formData := url.Values{
+		"key": {"key"},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get_check_key", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readCheckGood1(): 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("readCheckGood1(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+	if resp.StatusCode != 200 {
+		sf.t.Fatalf("readCheckGood1(): resp.StatusCode=%v", resp.StatusCode)
+	}
+}
+
+// Пустой анонимный ключ
+func (sf *tester) readCheckBad2() {
+	sf.t.Log("readCheckBad2")
+	app := fiber.New()
+	app.Post("/anonym/get_check_key", sf.anonRead.readCheckKey)
+	formData := url.Values{
+		"key": {},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get_check_key", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readCheckBad2(): 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("readCheckBad2(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+}
+
+// Кривой запрос
+func (sf *tester) readCheckBad1() {
+	sf.t.Log("readCheckBad1")
+	app := fiber.New()
+	app.Post("/anonym/get_check_key", sf.anonRead.readCheckKey)
+	formData := url.Values{
+		"key": {},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get_check_key", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readCheckBad1(): err=\n\t%v", err)
+	}
+	resp, err := app.Test(req)
+	if err != nil {
+		sf.t.Fatalf("readCheckBad1(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+}
+
+// Получает страницу получени анонимного ключа
+func (sf *tester) get() {
+	sf.t.Log("get")
+	app := fiber.New()
+	app.Get("/anonym/get", sf.anonRead.get)
+	req, err := http.NewRequest("GET", "/anonym/get", 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()
+}
+
+// Онлайн проверка значения ключа
+func (sf *tester) read() {
+	sf.t.Log("readCheck")
+	sf.readBad1()
+	sf.readBad2()
+	sf.readBad3()
+	sf.readGood1()
+}
+
+func (sf *tester) readGood1() {
+	sf.t.Log("readGood1")
+	app := fiber.New()
+	app.Post("/anonym/get", sf.anonRead.post)
+	formData := url.Values{
+		"key": {"key"},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readGood1(): err=\n\t%v", err)
+	}
+	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+	err = sf.store.Put("anon/key", []byte("value"))
+	if err != nil {
+		sf.t.Fatalf("readGood1(): err=\n\t%v", err)
+	}
+	resp, err := app.Test(req)
+	if err != nil {
+		sf.t.Fatalf("readGood1(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+	if resp.StatusCode != 200 {
+		sf.t.Fatalf("readGood1(): resp.Status=%v", resp.Status)
+	}
+}
+
+// Закрыто хранилище
+func (sf *tester) readBad3() {
+	sf.t.Log("readBad3")
+	app := fiber.New()
+	app.Post("/anonym/get", sf.anonRead.post)
+	formData := url.Values{
+		"key": {"key"},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readBad3(): 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("readBad3(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+	mock_store.IsBad_ = false
+}
+
+// Пустой ключ запроса
+func (sf *tester) readBad2() {
+	sf.t.Log("readBad2")
+	app := fiber.New()
+	app.Post("/anonym/get", sf.anonRead.post)
+	formData := url.Values{
+		"key": {""},
+	}
+	req, err := http.NewRequest("POST", "/anonym/get", strings.NewReader(formData.Encode()))
+	if err != nil {
+		sf.t.Fatalf("readBad2(): 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("readBad2(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+}
+
+// Кривой запрос
+func (sf *tester) readBad1() {
+	sf.t.Log("readBad1")
+	app := fiber.New()
+	app.Post("/anonym/get", sf.anonRead.post)
+	req, err := http.NewRequest("POST", "/anonym/get", nil)
+	if err != nil {
+		sf.t.Fatalf("readBad1(): err=\n\t%v", err)
+	}
+	resp, err := app.Test(req)
+	if err != nil {
+		sf.t.Fatalf("readBad1(): err=\n\t%v", err)
+	}
+	defer resp.Body.Close()
+}
+
+// Создание обработчика чтения анонимного ключа
+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.anonRead, err = NewAnonymRead(sf.web, sf.router)
+	if err != nil {
+		sf.t.Fatalf("createGood1(): err=\n\t%v", err)
+	}
+	if sf.anonRead == nil {
+		sf.t.Fatalf("createGood1(): anonRead==nil")
+	}
+}
+
+// Нет роутера
+func (sf *tester) createBad2() {
+	sf.t.Log("createBad2")
+	anonRead, err := NewAnonymRead(sf.web, nil)
+	if err == nil {
+		sf.t.Fatalf("createBad2(): err==nil")
+	}
+	if anonRead != nil {
+		sf.t.Fatalf("createBad2(): anonRead!=nil")
+	}
+}
+
+// Нет веб-сервера
+func (sf *tester) createBad1() {
+	sf.t.Log("createBad1")
+	sf.serv = mock_service.NewMockService()
+	var err error
+	sf.store, err = mock_store.NewMockStore(sf.serv)
+	if err != nil {
+		sf.t.Fatalf("createBad1(): err=\n\t%v", err)
+	}
+	sf.serv.Store_ = sf.store
+	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/get")
+	anonRead, err := NewAnonymRead(nil, sf.router)
+	if err == nil {
+		sf.t.Fatalf("createBad1(): err==nil")
+	}
+	if anonRead != nil {
+		sf.t.Fatalf("createBad1(): anonRead!=nil")
+	}
+}