Add rights and test stubs

This commit is contained in:
kolaente 2021-02-09 21:39:14 +01:00
parent b4e63307a7
commit fafeb9f29e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 204 additions and 0 deletions

View File

@ -1423,3 +1423,34 @@ func (err ErrSavedFilterNotAvailableForLinkShare) HTTPError() web.HTTPError {
Message: "Saved filters are not available for link shares.",
}
}
// =============
// Subscriptions
// =============
// ErrUnknownSubscriptionEntityType represents an error where a subscription entity type is unknown
type ErrUnknownSubscriptionEntityType struct {
EntityType SubscriptionEntityType
}
// IsErrUnknownSubscriptionEntityType checks if an error is ErrUnknownSubscriptionEntityType.
func IsErrUnknownSubscriptionEntityType(err error) bool {
_, ok := err.(*ErrUnknownSubscriptionEntityType)
return ok
}
func (err *ErrUnknownSubscriptionEntityType) Error() string {
return fmt.Sprintf("Subscription entity type is unkowns [EntityType: %d]", err.EntityType)
}
// ErrCodeUnknownSubscriptionEntityType holds the unique world-error code of this error
const ErrCodeUnknownSubscriptionEntityType = 12001
// HTTPError holds the http error description
func (err ErrUnknownSubscriptionEntityType) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusPreconditionFailed,
Code: ErrCodeUnknownSubscriptionEntityType,
Message: "The subscription entity type is invalid.",
}
}

View File

@ -59,6 +59,7 @@ func GetTables() []interface{} {
&Bucket{},
&UnsplashPhoto{},
&SavedFilter{},
&Subscription{},
}
}

View File

@ -27,6 +27,7 @@ import (
type SubscriptionEntityType int
const (
SubscriptionEntityUnknown = -1
SubscriptionEntityNamespace = iota
SubscriptionEntityList
SubscriptionEntityTask
@ -58,6 +59,19 @@ func (sb *Subscription) TableName() string {
return "subscriptions"
}
func getEntityTypeFromString(entityType string) SubscriptionEntityType {
switch entityType {
case "namespace":
return SubscriptionEntityNamespace
case "list":
return SubscriptionEntityList
case "task":
return SubscriptionEntityTask
}
return SubscriptionEntityUnknown
}
func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) error {
}

View File

@ -0,0 +1,52 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/web"
"xorm.io/xorm"
)
func (sb *Subscription) CanCreate(s *xorm.Session, a web.Auth) (can bool, err error) {
entityType := getEntityTypeFromString(sb.Entity)
switch entityType {
case SubscriptionEntityNamespace:
n := &Namespace{ID: sb.EntityID}
can, _, err = n.CanRead(s, a)
case SubscriptionEntityList:
l := &List{ID: sb.EntityID}
can, _, err = l.CanRead(s, a)
case SubscriptionEntityTask:
t := &Task{ID: sb.EntityID}
can, _, err = t.CanRead(s, a)
default:
return false, &ErrUnknownSubscriptionEntityType{EntityType: entityType}
}
return
}
func (sb *Subscription) CanDelete(s *xorm.Session, a web.Auth) (can bool, err error) {
realSb := &Subscription{}
_, err = s.Where("entity_type = ? AND entity_id = ?", sb.EntityType, sb.EntityID).Get(realSb)
if err != nil {
return false, err
}
return realSb.UserID == a.GetID(), nil
}

View File

@ -0,0 +1,106 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSubscriptionGetTypeFromString(t *testing.T) {
t.Run("namespace", func(t *testing.T) {
entityType := getEntityTypeFromString("namespace")
assert.Equal(t, SubscriptionEntityNamespace, entityType)
})
t.Run("list", func(t *testing.T) {
entityType := getEntityTypeFromString("list")
assert.Equal(t, SubscriptionEntityList, entityType)
})
t.Run("task", func(t *testing.T) {
entityType := getEntityTypeFromString("task")
assert.Equal(t, SubscriptionEntityTask, entityType)
})
t.Run("invalid", func(t *testing.T) {
entityType := getEntityTypeFromString("someomejghsd")
assert.Equal(t, SubscriptionEntityUnknown, entityType)
})
}
func TestSubscription_Create(t *testing.T) {
u := &user.User{ID: 1}
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("noneixsting namespace", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("noneixsting list", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("noneixsting task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("no rights to see namespace", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("no rights to see list", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("no rights to see task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
t.Run("Subscription already exists for this entity", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
// TODO: Add tests to test triggering of notifications for subscribed things
}
func TestSubscription_Delete(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
})
}