genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/http/good_test.go

131 lines
3.6 KiB
Go
Raw Normal View History

// Package that contains all api routes of this microservice
2017-04-29 18:26:36 +02:00
package http
import (
2017-05-19 11:47:38 +02:00
"fmt"
2017-04-29 18:26:36 +02:00
"net/http"
"testing"
"time"
"github.com/genofire/hs_master-kss-monolith/lib/database"
"github.com/genofire/hs_master-kss-monolith/models"
"github.com/genofire/hs_master-kss-monolith/runtime"
"github.com/genofire/hs_master-kss-monolith/test"
)
// Function to test addGood()
2017-04-29 18:26:36 +02:00
func TestAddGood(t *testing.T) {
assertion, router := test.Init(t)
BindAPI(router)
runtime.PermissionURL = "http://localhost:8080/api-test/session/%s/%d/"
session := test.NewSession(router)
good := models.Good{
ProductID: 3,
Comment: "blub",
}
_, w := session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusForbidden, w.StatusCode)
2017-04-29 18:26:36 +02:00
session.Login()
_, w = session.JSONRequest("POST", "/api/good/a", good)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
2017-05-07 15:38:08 +02:00
_, w = session.JSONRequest("POST", "/api/good/7", good)
2017-04-29 18:26:36 +02:00
assertion.Equal(http.StatusNotFound, w.StatusCode)
2017-05-19 11:02:19 +02:00
_, w = session.JSONRequest("POST", "/api/good/2", true)
2017-05-18 00:34:26 +02:00
assertion.Equal(http.StatusBadRequest, w.StatusCode)
2017-05-19 11:02:19 +02:00
_, w = session.JSONRequest("POST", "/api/good/2", good)
assertion.Equal(http.StatusOK, w.StatusCode)
var count int
database.Read.Model(&good).Where("product_id", 2).Count(&count)
assertion.Equal(1, count)
good = models.Good{
ProductID: 3,
Comment: "blub",
}
_, w = session.JSONRequest("POST", "/api/good/4?count=3", good)
2017-04-29 18:26:36 +02:00
assertion.Equal(http.StatusOK, w.StatusCode)
2017-05-19 11:02:19 +02:00
database.Read.Model(&good).Where("product_id", 4).Count(&count)
assertion.Equal(4, count)
2017-04-29 18:26:36 +02:00
database.Close()
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
session.Logout()
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusForbidden, w.StatusCode)
session.Login()
runtime.CacheConfig.After = models.Duration{Duration: time.Duration(5) * time.Millisecond}
test.CloseServer()
time.Sleep(time.Duration(10) * time.Millisecond)
runtime.HasPermission("testsessionkey", runtime.PermissionCreateGood)
runtime.CleanCache()
// Test the gatewaytimeout on product exists
2017-04-29 18:26:36 +02:00
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusGatewayTimeout, w.StatusCode)
time.Sleep(time.Duration(10) * time.Millisecond)
runtime.CleanCache()
// Test the gatewaytimeout on permission exists
2017-04-29 18:26:36 +02:00
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusGatewayTimeout, w.StatusCode)
test.Close()
}
2017-05-18 23:42:00 +02:00
// Function to test delGood()
func TestDelGood(t *testing.T) {
assertion, router := test.Init(t)
BindAPI(router)
runtime.PermissionURL = "http://localhost:8080/api-test/session/%s/%d/"
session := test.NewSession(router)
good := models.Good{
Comment: "blub",
}
database.Write.Create(&good)
_, w := session.JSONRequest("DELETE", "/api/good/1", nil)
assertion.Equal(http.StatusForbidden, w.StatusCode)
2017-05-18 23:42:00 +02:00
session.Login()
_, w = session.JSONRequest("DELETE", "/api/good/a", nil)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
2017-05-19 11:02:19 +02:00
_, w = session.JSONRequest("DELETE", fmt.Sprintf("/api/good/%d", good.ID), nil)
2017-05-18 23:42:00 +02:00
assertion.Equal(http.StatusOK, w.StatusCode)
2017-05-19 11:02:19 +02:00
_, w = session.JSONRequest("DELETE", fmt.Sprintf("/api/good/%d", good.ID), nil)
2017-05-18 23:42:00 +02:00
assertion.Equal(http.StatusNotFound, w.StatusCode)
2017-05-19 12:41:07 +02:00
time.Sleep(time.Millisecond)
2017-05-18 23:42:00 +02:00
database.Close()
_, w = session.JSONRequest("DELETE", "/api/good/1", nil)
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
session.Logout()
_, w = session.JSONRequest("DELETE", "/api/good/1", nil)
assertion.Equal(http.StatusForbidden, w.StatusCode)
test.Close()
}