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

117 lines
3.2 KiB
Go

// Package that contains all api routes of this microservice
package http
import (
"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()
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.StatusNonAuthoritativeInfo, w.StatusCode)
session.Login()
_, w = session.JSONRequest("POST", "/api/good/a", good)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
_, w = session.JSONRequest("POST", "/api/good/7", good)
assertion.Equal(http.StatusNotFound, w.StatusCode)
_, w = session.JSONRequest("POST", "/api/good/1", true)
assertion.Equal(http.StatusBadRequest, w.StatusCode)
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusOK, w.StatusCode)
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 gatewaytimeout on product exists
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusGatewayTimeout, w.StatusCode)
time.Sleep(time.Duration(10) * time.Millisecond)
runtime.CleanCache()
// Test gatewaytimeout on permission exists
_, w = session.JSONRequest("POST", "/api/good/1", good)
assertion.Equal(http.StatusGatewayTimeout, w.StatusCode)
test.Close()
}
// 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{
ID: 3,
Comment: "blub",
}
database.Write.Create(&good)
_, w := session.JSONRequest("DELETE", "/api/good/1", nil)
assertion.Equal(http.StatusNonAuthoritativeInfo, w.StatusCode)
session.Login()
_, w = session.JSONRequest("DELETE", "/api/good/a", nil)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
_, w = session.JSONRequest("DELETE", "/api/good/3", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
_, w = session.JSONRequest("DELETE", "/api/good/3", nil)
assertion.Equal(http.StatusNotFound, w.StatusCode)
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()
}