Add tests
This commit is contained in:
parent
ee5bade0fb
commit
cdab0c9a60
|
@ -0,0 +1,5 @@
|
|||
- id: 1
|
||||
entity_type: 3 # Task
|
||||
entity_id: 2
|
||||
user_id: 1
|
||||
created: 2018-12-01 15:13:12
|
|
@ -27,8 +27,8 @@ import (
|
|||
type SubscriptionEntityType int
|
||||
|
||||
const (
|
||||
SubscriptionEntityUnknown = -1
|
||||
SubscriptionEntityNamespace = iota
|
||||
SubscriptionEntityUnknown = iota
|
||||
SubscriptionEntityNamespace
|
||||
SubscriptionEntityList
|
||||
SubscriptionEntityTask
|
||||
)
|
||||
|
@ -90,6 +90,11 @@ func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) (err error) {
|
|||
}
|
||||
|
||||
_, err = s.Insert(sb)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sb.User, err = user.GetFromAuth(auth)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -22,9 +22,13 @@ import (
|
|||
)
|
||||
|
||||
func (sb *Subscription) CanCreate(s *xorm.Session, a web.Auth) (can bool, err error) {
|
||||
entityType := getEntityTypeFromString(sb.Entity)
|
||||
if _, is := a.(*LinkSharing); is {
|
||||
return false, &ErrGenericForbidden{}
|
||||
}
|
||||
|
||||
switch entityType {
|
||||
sb.EntityType = getEntityTypeFromString(sb.Entity)
|
||||
|
||||
switch sb.EntityType {
|
||||
case SubscriptionEntityNamespace:
|
||||
n := &Namespace{ID: sb.EntityID}
|
||||
can, _, err = n.CanRead(s, a)
|
||||
|
@ -35,15 +39,23 @@ func (sb *Subscription) CanCreate(s *xorm.Session, a web.Auth) (can bool, err er
|
|||
t := &Task{ID: sb.EntityID}
|
||||
can, _, err = t.CanRead(s, a)
|
||||
default:
|
||||
return false, &ErrUnknownSubscriptionEntityType{EntityType: entityType}
|
||||
return false, &ErrUnknownSubscriptionEntityType{EntityType: sb.EntityType}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (sb *Subscription) CanDelete(s *xorm.Session, a web.Auth) (can bool, err error) {
|
||||
if _, is := a.(*LinkSharing); is {
|
||||
return false, &ErrGenericForbidden{}
|
||||
}
|
||||
|
||||
sb.EntityType = getEntityTypeFromString(sb.Entity)
|
||||
|
||||
realSb := &Subscription{}
|
||||
_, err = s.Where("entity_type = ? AND entity_id = ?", sb.EntityType, sb.EntityID).Get(realSb)
|
||||
_, err = s.
|
||||
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, sb.UserID).
|
||||
Get(realSb)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package models
|
|||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
@ -25,19 +26,19 @@ import (
|
|||
func TestSubscriptionGetTypeFromString(t *testing.T) {
|
||||
t.Run("namespace", func(t *testing.T) {
|
||||
entityType := getEntityTypeFromString("namespace")
|
||||
assert.Equal(t, SubscriptionEntityNamespace, entityType)
|
||||
assert.Equal(t, SubscriptionEntityType(SubscriptionEntityNamespace), entityType)
|
||||
})
|
||||
t.Run("list", func(t *testing.T) {
|
||||
entityType := getEntityTypeFromString("list")
|
||||
assert.Equal(t, SubscriptionEntityList, entityType)
|
||||
assert.Equal(t, SubscriptionEntityType(SubscriptionEntityList), entityType)
|
||||
})
|
||||
t.Run("task", func(t *testing.T) {
|
||||
entityType := getEntityTypeFromString("task")
|
||||
assert.Equal(t, SubscriptionEntityTask, entityType)
|
||||
assert.Equal(t, SubscriptionEntityType(SubscriptionEntityTask), entityType)
|
||||
})
|
||||
t.Run("invalid", func(t *testing.T) {
|
||||
entityType := getEntityTypeFromString("someomejghsd")
|
||||
assert.Equal(t, SubscriptionEntityUnknown, entityType)
|
||||
assert.Equal(t, SubscriptionEntityType(SubscriptionEntityUnknown), entityType)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -48,48 +49,155 @@ func TestSubscription_Create(t *testing.T) {
|
|||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 1,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, can)
|
||||
|
||||
err = sb.Create(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, sb.User)
|
||||
|
||||
db.AssertExists(t, "subscriptions", map[string]interface{}{
|
||||
"entity_type": 3,
|
||||
"entity_id": 1,
|
||||
"user_id": u.ID,
|
||||
}, false)
|
||||
})
|
||||
t.Run("forbidden for link shares", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
linkShare := &LinkSharing{}
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 1,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, linkShare)
|
||||
assert.Error(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("noneixsting namespace", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "namespace",
|
||||
EntityID: 99999999,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrNamespaceDoesNotExist(err))
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("noneixsting list", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "list",
|
||||
EntityID: 99999999,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrListDoesNotExist(err))
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("noneixsting task", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 99999999,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrTaskDoesNotExist(err))
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("no rights to see namespace", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "namespace",
|
||||
EntityID: 6,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("no rights to see list", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "list",
|
||||
EntityID: 20,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("no rights to see task", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 14,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("existing subscription for (entity_id, entity_type, user_id) ", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 2,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanCreate(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, can)
|
||||
|
||||
err = sb.Create(s, u)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrSubscriptionAlreadyExists(err))
|
||||
})
|
||||
|
||||
// TODO: Add tests to test triggering of notifications for subscribed things
|
||||
|
@ -101,5 +209,56 @@ func TestSubscription_Delete(t *testing.T) {
|
|||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
u := &user.User{ID: 1}
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 2,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanDelete(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, can)
|
||||
|
||||
err = sb.Delete(s, u)
|
||||
assert.NoError(t, err)
|
||||
db.AssertMissing(t, "subscriptions", map[string]interface{}{
|
||||
"entity_type": 3,
|
||||
"entity_id": 2,
|
||||
"user_id": u.ID,
|
||||
})
|
||||
})
|
||||
t.Run("forbidden for link shares", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
linkShare := &LinkSharing{}
|
||||
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 1,
|
||||
UserID: 1,
|
||||
}
|
||||
|
||||
can, err := sb.CanDelete(s, linkShare)
|
||||
assert.Error(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
t.Run("not owner of the subscription", func(t *testing.T) {
|
||||
db.LoadAndAssertFixtures(t)
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
u := &user.User{ID: 2}
|
||||
sb := &Subscription{
|
||||
Entity: "task",
|
||||
EntityID: 2,
|
||||
UserID: u.ID,
|
||||
}
|
||||
|
||||
can, err := sb.CanDelete(s, u)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, can)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ func SetupTests() {
|
|||
"users_namespace",
|
||||
"buckets",
|
||||
"saved_filters",
|
||||
"subscriptions",
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
Loading…
Reference in New Issue