util_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package engine
  11. import "testing"
  12. func TestRuleScope(t *testing.T) {
  13. rs := NewRuleScope(map[string]bool{
  14. "test.first.read": true,
  15. "test.first.write": false,
  16. "test.second": true,
  17. })
  18. if rs.IsAllowed("test.first") {
  19. t.Error("Unexpected result")
  20. return
  21. }
  22. if rs.IsAllowed("test.first.write") {
  23. t.Error("Unexpected result")
  24. return
  25. }
  26. if !rs.IsAllowed("test.first.read") {
  27. t.Error("Unexpected result")
  28. return
  29. }
  30. if !rs.IsAllowed("test.second") {
  31. t.Error("Unexpected result")
  32. return
  33. }
  34. if !rs.IsAllowed("test.second.bla") {
  35. t.Error("Unexpected result")
  36. return
  37. }
  38. // Test all is allowed
  39. rs = NewRuleScope(map[string]bool{
  40. "": true,
  41. })
  42. if !rs.IsAllowed("test.first") {
  43. t.Error("Unexpected result")
  44. return
  45. }
  46. if !rs.IsAllowed("test.first.write") {
  47. t.Error("Unexpected result")
  48. return
  49. }
  50. // Test nothing is allowed
  51. rs = NewRuleScope(nil)
  52. if rs.IsAllowed("test.first") {
  53. t.Error("Unexpected result")
  54. return
  55. }
  56. if rs.IsAllowed("test.first.write") {
  57. t.Error("Unexpected result")
  58. return
  59. }
  60. }