types_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package user
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. )
  6. func TestPermission(t *testing.T) {
  7. require.Equal(t, PermissionReadWrite, NewPermission(true, true))
  8. require.Equal(t, PermissionRead, NewPermission(true, false))
  9. require.Equal(t, PermissionWrite, NewPermission(false, true))
  10. require.Equal(t, PermissionDenyAll, NewPermission(false, false))
  11. require.True(t, PermissionReadWrite.IsReadWrite())
  12. require.True(t, PermissionReadWrite.IsRead())
  13. require.True(t, PermissionReadWrite.IsWrite())
  14. require.True(t, PermissionRead.IsRead())
  15. require.True(t, PermissionWrite.IsWrite())
  16. }
  17. func TestParsePermission(t *testing.T) {
  18. _, err := ParsePermission("no")
  19. require.NotNil(t, err)
  20. p, err := ParsePermission("read-write")
  21. require.Nil(t, err)
  22. require.Equal(t, PermissionReadWrite, p)
  23. p, err = ParsePermission("rw")
  24. require.Nil(t, err)
  25. require.Equal(t, PermissionReadWrite, p)
  26. p, err = ParsePermission("read-only")
  27. require.Nil(t, err)
  28. require.Equal(t, PermissionRead, p)
  29. p, err = ParsePermission("WRITE")
  30. require.Nil(t, err)
  31. require.Equal(t, PermissionWrite, p)
  32. p, err = ParsePermission("deny-all")
  33. require.Nil(t, err)
  34. require.Equal(t, PermissionDenyAll, p)
  35. }
  36. func TestAllowedTier(t *testing.T) {
  37. require.False(t, AllowedTier(" no"))
  38. require.True(t, AllowedTier("yes"))
  39. }
  40. func TestTierContext(t *testing.T) {
  41. tier := &Tier{
  42. ID: "ti_abc",
  43. Code: "pro",
  44. StripePriceID: "price_123",
  45. }
  46. context := tier.Context()
  47. require.Equal(t, "ti_abc", context["tier_id"])
  48. require.Equal(t, "pro", context["tier_code"])
  49. require.Equal(t, "price_123", context["stripe_price_id"])
  50. }