Skip to content

Commit

Permalink
test(api) add more entity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aguszorza committed Apr 3, 2024
1 parent f702073 commit 1b7d681
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion API/controllers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestGetAllUsers(t *testing.T) {

data, exists := response["data"].([]interface{})
assert.True(t, exists)
assert.Equal(t, 3, len(data))
assert.Equal(t, 5, len(data))
}

func TestGetUsersWithNormalUser(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions API/controllers/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,10 @@ func GetLayerObjects(w http.ResponseWriter, r *http.Request) {
data, modelErr = models.GetObject(bson.M{"slug": id}, u.EntityToString(u.LAYER), u.RequestFilters{}, user.Roles)
if modelErr != nil {
u.RespondWithError(w, modelErr)
return
} else if len(data) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}

// Apply layer to get objects request
Expand Down
59 changes: 56 additions & 3 deletions API/controllers/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,15 @@ func TestGetTemperature(t *testing.T) {
}

// Tests get subentities
func TestErrorGetRoomsSites(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/rooms/site-no-temperature.building-2.room-1/sites", nil)
assert.Equal(t, http.StatusNotFound, recorder.Code)
func TestErrorGetRoomsBuildingsInvalidHierarchy(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/rooms/site-no-temperature.building-1.room-1/buildings", nil)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
message, exists := response["message"].(string)
assert.True(t, exists)
assert.Equal(t, "Invalid set of entities in URL: first entity should be parent of the second entity", message)
}

func TestErrorGetSiteRoomsUnknownEntity(t *testing.T) {
Expand Down Expand Up @@ -736,6 +742,36 @@ func TestValidateValidRoomEntity(t *testing.T) {
assert.Equal(t, "This object can be created", message)
}

func TestErrorValidateValidRoomEntityNotEnoughPermissions(t *testing.T) {
requestBody := []byte(`{
"attributes": {
"floorUnit": "t",
"height": "2.8",
"heightUnit": "m",
"axisOrientation": "+x+y",
"rotation": "-90",
"posXY": "[0, 0]",
"posXYUnit": "m",
"size": "[-13, -2.9]",
"sizeUnit": "m",
"template": ""
},
"category": "room",
"description": "room",
"domain": "` + integration.TestDBName + `",
"name": "roomA",
"parentId": "site-no-temperature.building-1"
}`)
recorder := e2e.MakeRequestWithUser("POST", "/api/validate/rooms", requestBody, "viewer")
assert.Equal(t, http.StatusUnauthorized, recorder.Code)

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
message, exists := response["message"].(string)
assert.True(t, exists)
assert.Equal(t, "This user does not have sufficient permissions to create this object under this domain ", message)
}

func TestGetStats(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/stats", nil)
assert.Equal(t, http.StatusOK, recorder.Code)
Expand Down Expand Up @@ -764,6 +800,23 @@ func TestGetApiVersion(t *testing.T) {
assert.True(t, len(customer) > 0)
}

// Tests layers objects
func TestGetLayersObjectsRootRequired(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/layers/racks-layer/objects", nil)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
message, exists := response["message"].(string)
assert.True(t, exists)
assert.Equal(t, "Query param root is mandatory", message)
}

func TestGetLayersObjectsLayerUnknown(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/layers/unknown/objects?root=site-no-temperature.building-1.room-1", nil)
assert.Equal(t, http.StatusNotFound, recorder.Code)
}

func TestGetLayersObjectsWithSimpleFilter(t *testing.T) {
recorder := e2e.MakeRequest("GET", "/api/layers/racks-layer/objects?root=site-no-temperature.building-1.room-1", nil)
assert.Equal(t, http.StatusOK, recorder.Code)
Expand Down
37 changes: 22 additions & 15 deletions API/test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ import (

"github.com/elliotchance/pie/v2"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var appRouter *mux.Router
var AdminId primitive.ObjectID
var AdminToken string
var users map[string]any

func init() {
appRouter = router.Router(app.JwtAuthentication)
createAdminAccount()
users = map[string]any{}
createUser("admin", map[string]models.Role{"*": "manager"})
createUser("user", map[string]models.Role{"*": "user"})
createUser("viewer", map[string]models.Role{"*": "viewer"})
}

func createAdminAccount() {
// Create admin account
admin := models.Account{}
admin.Email = "[email protected]"
admin.Password = "admin123"
admin.Roles = map[string]models.Role{"*": "manager"}
func createUser(userType string, role map[string]models.Role) {
user := models.Account{}
user.Email = userType + "@" + userType + ".com"
user.Password = userType + "123"
user.Roles = role

newAcc, err := admin.Create(map[string]models.Role{"*": "manager"})
newAcc, err := user.Create(map[string]models.Role{"*": "manager"})
if err != nil {
log.Fatalln("Error while creating admin account:", err.Error())
log.Fatalln("Error while creating "+userType+"account:", err.Error())
}

if newAcc != nil {
AdminId = newAcc.ID
AdminToken = newAcc.Token
users[userType] = map[string]any{
"id": newAcc.ID,
"token": newAcc.Token,
}
}
}

Expand All @@ -60,8 +62,13 @@ func MakeRequestWithToken(method, url string, requestBody []byte, token string)
return MakeRequestWithHeaders(method, url, requestBody, header)
}

func MakeRequestWithUser(method, url string, requestBody []byte, user string) *httptest.ResponseRecorder {
token := users[user].(map[string]any)["token"].(string)
return MakeRequestWithToken(method, url, requestBody, token)
}

func MakeRequest(method, url string, requestBody []byte) *httptest.ResponseRecorder {
return MakeRequestWithToken(method, url, requestBody, AdminToken)
return MakeRequestWithUser(method, url, requestBody, "admin")
}

func GetObjects(queryParams string) (*httptest.ResponseRecorder, []map[string]any) {
Expand Down

0 comments on commit 1b7d681

Please sign in to comment.