1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-09 13:24:24 +01:00
ntfy/user/types_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.1 KiB
Go
Raw Normal View History

2023-02-09 04:57:10 +01:00
package user
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestPermission(t *testing.T) {
require.Equal(t, PermissionReadWrite, NewPermission(true, true))
require.Equal(t, PermissionRead, NewPermission(true, false))
require.Equal(t, PermissionWrite, NewPermission(false, true))
require.Equal(t, PermissionDenyAll, NewPermission(false, false))
2023-02-12 04:14:09 +01:00
require.True(t, PermissionReadWrite.IsReadWrite())
require.True(t, PermissionReadWrite.IsRead())
require.True(t, PermissionReadWrite.IsWrite())
require.True(t, PermissionRead.IsRead())
require.True(t, PermissionWrite.IsWrite())
}
func TestParsePermission(t *testing.T) {
_, err := ParsePermission("no")
require.NotNil(t, err)
p, err := ParsePermission("read-write")
require.Nil(t, err)
require.Equal(t, PermissionReadWrite, p)
p, err = ParsePermission("rw")
require.Nil(t, err)
require.Equal(t, PermissionReadWrite, p)
p, err = ParsePermission("read-only")
require.Nil(t, err)
require.Equal(t, PermissionRead, p)
p, err = ParsePermission("WRITE")
require.Nil(t, err)
require.Equal(t, PermissionWrite, p)
p, err = ParsePermission("deny-all")
require.Nil(t, err)
require.Equal(t, PermissionDenyAll, p)
}
func TestAllowedTier(t *testing.T) {
require.False(t, AllowedTier(" no"))
require.True(t, AllowedTier("yes"))
}
func TestTierContext(t *testing.T) {
tier := &Tier{
2023-02-22 04:44:30 +01:00
ID: "ti_abc",
Code: "pro",
StripeMonthlyPriceID: "price_123",
StripeYearlyPriceID: "price_456",
2023-02-12 04:14:09 +01:00
}
context := tier.Context()
require.Equal(t, "ti_abc", context["tier_id"])
require.Equal(t, "pro", context["tier_code"])
2023-02-22 04:44:30 +01:00
require.Equal(t, "price_123", context["stripe_monthly_price_id"])
require.Equal(t, "price_456", context["stripe_yearly_price_id"])
2023-02-09 04:57:10 +01:00
}
2024-04-04 05:58:29 +02:00
func TestUsernameRegex(t *testing.T) {
username := "phil"
2024-04-08 05:40:24 +02:00
usernameEmail := "phil@ntfy.sh"
usernameEmailAlias := "phil+alias@ntfy.sh"
usernameInvalid := "phil\rocks"
2024-04-04 05:58:29 +02:00
require.True(t, AllowedUsername(username))
2024-04-08 05:40:24 +02:00
require.True(t, AllowedUsername(usernameEmail))
require.True(t, AllowedUsername(usernameEmailAlias))
require.False(t, AllowedUsername(usernameInvalid))
2024-04-04 05:58:29 +02:00
}