f1909da1ad
Go CI / build (push) Failing after 2m42s
feat: create basic server to manage google oauth, account, sessions, places, attributes and ratings.
95 lines
3.1 KiB
Go
95 lines
3.1 KiB
Go
package test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.pengzhan.dev/noteplace-server/internal/models"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestAttributesAPI(t *testing.T) {
|
|
logger, _ := zap.NewDevelopment()
|
|
zap.ReplaceGlobals(logger)
|
|
|
|
router, s := setupTestRouter(t)
|
|
|
|
adminSessionID := createTestUser(t, s, adminUser)
|
|
user1SessionID := createTestUser(t, s, regularUser1)
|
|
|
|
// Create a dummy place for attribute testing
|
|
dummyPlace := models.Place{ID: "test-place-attr", Name: "Test Place for Attributes", Category: "Restaurant"}
|
|
_ = s.CreatePlace(dummyPlace)
|
|
|
|
t.Run("Admin can create an attribute", func(t *testing.T) {
|
|
newAttribute := models.Attribute{
|
|
Name: "Outdoor Seating",
|
|
Scope: models.CATEGORY,
|
|
OwnerID: "Restaurant",
|
|
Description: "Has outdoor seating area",
|
|
}
|
|
w := performRequest(router, "POST", "/api/attributes", newAttribute, adminSessionID)
|
|
assert.Equal(t, http.StatusCreated, w.Code)
|
|
|
|
var createdAttribute models.Attribute
|
|
err := json.Unmarshal(w.Body.Bytes(), &createdAttribute)
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, createdAttribute.ID)
|
|
assert.Equal(t, newAttribute.Name, createdAttribute.Name)
|
|
|
|
// Verify it's in the store
|
|
foundAttrs := s.GetAttributesByPlace(dummyPlace)
|
|
assert.Contains(t, foundAttrs, createdAttribute)
|
|
})
|
|
|
|
t.Run("Regular user cannot create an attribute", func(t *testing.T) {
|
|
newAttribute := models.Attribute{
|
|
Name: "Pet Friendly",
|
|
Scope: models.PLACE,
|
|
OwnerID: dummyPlace.ID,
|
|
Description: "Allows pets inside",
|
|
}
|
|
w := performRequest(router, "POST", "/api/attributes", newAttribute, user1SessionID)
|
|
assert.Equal(t, http.StatusUnauthorized, w.Code) // Expect 401 as per current API implementation
|
|
})
|
|
|
|
t.Run("Get attributes for a place", func(t *testing.T) {
|
|
// Create some attributes directly in the store
|
|
attr1 := models.Attribute{ID: "attr-1", Name: "Wifi", Scope: models.CATEGORY, OwnerID: "Restaurant"}
|
|
attr2 := models.Attribute{ID: "attr-2", Name: "Live Music", Scope: models.PLACE, OwnerID: dummyPlace.ID}
|
|
attr3 := models.Attribute{ID: "attr-3", Name: "Parking", Scope: models.CATEGORY, OwnerID: "Cafe"}
|
|
|
|
_ = s.CreateAttribute(attr1)
|
|
_ = s.CreateAttribute(attr2)
|
|
_ = s.CreateAttribute(attr3)
|
|
|
|
w := performRequest(router, "GET", fmt.Sprintf("/api/places/%s/attributes", dummyPlace.ID), nil, "")
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
var attributes []models.Attribute
|
|
err := json.Unmarshal(w.Body.Bytes(), &attributes)
|
|
assert.NoError(t, err)
|
|
|
|
// The admin created attribute, attr1, and attr2 should be present
|
|
assert.Len(t, attributes, 3)
|
|
assert.Contains(t, attributes, attr1)
|
|
assert.Contains(t, attributes, attr2)
|
|
|
|
// Check for the attribute created by admin in the previous test
|
|
foundAdminAttr := false
|
|
for _, attr := range attributes {
|
|
if attr.Name == "Outdoor Seating" {
|
|
foundAdminAttr = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, foundAdminAttr, "Admin created attribute not found")
|
|
|
|
w = performRequest(router, "GET", "/api/places/non-existent/attributes", nil, "")
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
})
|
|
}
|