Tests: Add unit tests #3943

This commit is contained in:
graciousgrey 2023-12-15 13:16:32 +01:00
parent 8e81f78c12
commit ee8de5e39f
4 changed files with 173 additions and 0 deletions

View file

@ -0,0 +1,19 @@
package acl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGrant_Allow(t *testing.T) {
t.Run("ViewAllDownload", func(t *testing.T) {
assert.True(t, GrantViewAll.Allow(ActionView))
})
t.Run("ViewAllShare", func(t *testing.T) {
assert.False(t, GrantViewAll.Allow(ActionShare))
})
t.Run("UnknownAction", func(t *testing.T) {
assert.False(t, GrantViewAll.Allow("lovecats"))
})
}

View file

@ -20,3 +20,30 @@ func TestPermissions_String(t *testing.T) {
assert.Equal(t, "manage, upload, access all", perms.String())
})
}
func TestPermission_LogId(t *testing.T) {
t.Run("FullAccess", func(t *testing.T) {
assert.Equal(t, "full access", FullAccess.LogId())
})
t.Run("ActionUpload", func(t *testing.T) {
assert.Equal(t, "upload", ActionUpload.LogId())
})
}
func TestPermission_Equal(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, FullAccess.Equal("full access"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, ActionUpload.Equal("full access"))
})
}
func TestPermission_NotEqual(t *testing.T) {
t.Run("False", func(t *testing.T) {
assert.False(t, FullAccess.NotEqual("full access"))
})
t.Run("True", func(t *testing.T) {
assert.True(t, ActionUpload.NotEqual("full access"))
})
}

View file

@ -0,0 +1,49 @@
package acl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestResource_String(t *testing.T) {
t.Run("Albums", func(t *testing.T) {
assert.Equal(t, "albums", ResourceAlbums.String())
})
t.Run("Favorites", func(t *testing.T) {
assert.Equal(t, "favorites", ResourceFavorites.String())
})
t.Run("Files", func(t *testing.T) {
assert.Equal(t, "files", ResourceFiles.String())
})
}
func TestResource_LogId(t *testing.T) {
t.Run("Albums", func(t *testing.T) {
assert.Equal(t, "albums", ResourceAlbums.LogId())
})
t.Run("Favorites", func(t *testing.T) {
assert.Equal(t, "favorites", ResourceFavorites.LogId())
})
t.Run("Files", func(t *testing.T) {
assert.Equal(t, "files", ResourceFiles.LogId())
})
}
func TestResource_Equal(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, ResourceAlbums.Equal("albums"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, ResourceAlbums.Equal("favorites"))
})
}
func TestResource_NotEqual(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, ResourceAlbums.NotEqual("favorites"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, ResourceAlbums.NotEqual("albums"))
})
}

78
internal/acl/role_test.go Normal file
View file

@ -0,0 +1,78 @@
package acl
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestRole_String(t *testing.T) {
t.Run("Admin", func(t *testing.T) {
assert.Equal(t, "admin", RoleAdmin.String())
})
t.Run("Client", func(t *testing.T) {
assert.Equal(t, "client", RoleClient.String())
})
t.Run("Visitor", func(t *testing.T) {
assert.Equal(t, "visitor", RoleVisitor.String())
})
}
func TestRole_Pretty(t *testing.T) {
t.Run("Admin", func(t *testing.T) {
assert.Equal(t, "Admin", RoleAdmin.Pretty())
})
t.Run("Unknown", func(t *testing.T) {
assert.Equal(t, "Unknown", RoleUnknown.Pretty())
})
t.Run("Visitor", func(t *testing.T) {
assert.Equal(t, "Visitor", RoleVisitor.Pretty())
})
}
func TestRole_LogId(t *testing.T) {
t.Run("Admin", func(t *testing.T) {
assert.Equal(t, "role admin", RoleAdmin.LogId())
})
t.Run("Client", func(t *testing.T) {
assert.Equal(t, "role client", RoleClient.LogId())
})
t.Run("Visitor", func(t *testing.T) {
assert.Equal(t, "role visitor", RoleVisitor.LogId())
})
}
func TestRole_Equal(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, RoleAdmin.Equal("admin"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, RoleClient.Equal("admin"))
})
}
func TestRole_NotEqual(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, RoleClient.NotEqual("admin"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, RoleAdmin.NotEqual("admin"))
})
}
func TestRole_Valid(t *testing.T) {
t.Run("True", func(t *testing.T) {
assert.True(t, RoleAdmin.Valid("admin"))
})
t.Run("False", func(t *testing.T) {
assert.False(t, RoleClient.Valid("client"))
})
}
func TestRole_Invalid(t *testing.T) {
t.Run("False", func(t *testing.T) {
assert.False(t, RoleAdmin.Invalid("admin"))
})
t.Run("True", func(t *testing.T) {
assert.True(t, RoleClient.Invalid("client"))
})
}