You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
package users
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func usersMock(t *testing.T) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
app := gin.Default()
|
|
// create token
|
|
return app
|
|
}
|
|
|
|
func TestUsersEndpoint(t *testing.T) {
|
|
app := usersMock(t)
|
|
api := app.Group("/api/v1")
|
|
ApplyRoutes(api)
|
|
request, err := http.NewRequest("GET", "/api/v1/users", nil)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
// Unauthorized
|
|
response := httptest.NewRecorder()
|
|
app.ServeHTTP(response, request)
|
|
if response.Result().StatusCode != http.StatusUnauthorized {
|
|
t.Errorf("api users - http status code must be \"%d\" (but is \"%d\") because of unauthorized request", http.StatusUnauthorized, response.Result().StatusCode)
|
|
}
|
|
body := response.Body.String()
|
|
var j map[string]interface{}
|
|
if err := json.Unmarshal([]byte(body), &j); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, exists := j["success"]; !exists {
|
|
t.Error("api users - success flag needs always be given")
|
|
} else if success, ok := j["success"].(bool); !ok {
|
|
t.Error("api users - success flag needs to be boolean")
|
|
} else if success {
|
|
t.Error("api users - success flag needs to be false because of unauthorized request")
|
|
}
|
|
// Create user
|
|
// request.Header.Set("Authorization", "Bearer "+token)
|
|
// t.Error(response)
|
|
}
|