2017-06-05 14:30:31 +02:00
|
|
|
// 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/test"
|
|
|
|
)
|
|
|
|
|
2017-06-21 15:25:18 +02:00
|
|
|
// Function to test lockGoods()
|
2017-06-05 14:51:01 +02:00
|
|
|
func TestLockGoods(t *testing.T) {
|
|
|
|
assertion, router := test.Init(t)
|
|
|
|
good := &models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blabla",
|
|
|
|
}
|
|
|
|
database.Write.Create(good)
|
|
|
|
|
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
|
|
|
_, w := session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
session.Header["secret"] = "hiddenLockTest"
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", 13)
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", nil)
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 0, Count: 2}})
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 2}})
|
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
result, w := session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
resultMap := result.(map[string]interface{})
|
|
|
|
count := resultMap["count"]
|
|
|
|
assertion.Equal(float64(1), count)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
database.Close()
|
|
|
|
|
|
|
|
assertion.Panics(func() {
|
|
|
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
test.Close()
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:25:18 +02:00
|
|
|
// Function to test releaseGoods()
|
2017-06-23 17:09:03 +02:00
|
|
|
func TestReleaseLockGoods(t *testing.T) {
|
|
|
|
assertion, router := test.Init(t)
|
|
|
|
good := &models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blabla",
|
|
|
|
LockedSecret: "hiddenLockTest",
|
|
|
|
}
|
|
|
|
database.Write.Create(good)
|
|
|
|
|
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
|
|
|
_, w := session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
session.Header["secret"] = "hiddenLockTest"
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", 13)
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", nil)
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 0, Count: 2}})
|
|
|
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 2}})
|
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
result, w := session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
resultBool := result.(bool)
|
|
|
|
assertion.True(resultBool)
|
|
|
|
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
database.Close()
|
|
|
|
|
|
|
|
assertion.Panics(func() {
|
|
|
|
_, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
|
|
|
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
test.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelLockGoods(t *testing.T) {
|
2017-06-05 14:30:31 +02:00
|
|
|
now := time.Now()
|
|
|
|
assertion, router := test.Init(t)
|
|
|
|
|
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blub",
|
|
|
|
LockedAt: &now,
|
|
|
|
LockedSecret: "hidden",
|
|
|
|
})
|
|
|
|
|
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
|
|
|
session.Header["secret"] = "a"
|
2017-06-23 17:09:03 +02:00
|
|
|
result, w := session.JSONRequest("PUT", "/api/goods/locking", nil)
|
2017-06-05 14:30:31 +02:00
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
session.Header["secret"] = "hidden"
|
2017-06-23 17:09:03 +02:00
|
|
|
result, w = session.JSONRequest("PUT", "/api/goods/locking", nil)
|
2017-06-05 14:30:31 +02:00
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
resultMap := result.(map[string]interface{})
|
|
|
|
count := resultMap["count"]
|
|
|
|
assertion.Equal(float64(1), count)
|
|
|
|
|
|
|
|
database.Close()
|
2017-06-23 17:09:03 +02:00
|
|
|
result, w = session.JSONRequest("PUT", "/api/goods/locking", nil)
|
2017-06-05 14:30:31 +02:00
|
|
|
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
|
|
|
|
|
|
|
|
test.Close()
|
|
|
|
|
|
|
|
}
|