| 1 | package accesscontrol |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestACLValidate_WithValidACL(t *testing.T) { |
| 10 | acl := ACL{ |
| 11 | Entries: []ACE{ |
| 12 | { |
| 13 | TagType: TagTypeUser, |
| 14 | Permissions: "rwx", |
| 15 | }, |
| 16 | { |
| 17 | TagType: TagTypeGroup, |
| 18 | Permissions: "r-x", |
| 19 | }, |
| 20 | { |
| 21 | TagType: TagTypeOther, |
| 22 | Permissions: "---", |
| 23 | }, |
| 24 | }, |
| 25 | } |
| 26 | assert.Nil(t, acl.Validate(), "Expected ACE to Validate successfully") |
| 27 | } |
| 28 | |
| 29 | func TestACLValidate_WithInvalidACL(t *testing.T) { |
| 30 | acl := ACL{ |
| 31 | Entries: []ACE{ |
| 32 | { |
| 33 | TagType: TagType("wibble"), |
| 34 | Permissions: "rwx", |
| 35 | }, |
| 36 | { |
| 37 | TagType: TagTypeGroup, |
| 38 | Permissions: "r-x", |
| 39 | }, |
| 40 | { |
| 41 | TagType: TagTypeOther, |
| 42 | Permissions: "---", |
| 43 | }, |
| 44 | }, |
| 45 | } |
| 46 | assert.Error(t, acl.Validate(), "Expected ACE to fail to Validate") |
| 47 | } |
| 48 | |
| 49 | func TestACLString(t *testing.T) { |
| 50 | acl := ACL{ |
| 51 | Entries: []ACE{ |
| 52 | { |
| 53 | TagType: TagTypeUser, |
| 54 | Permissions: "rwx", |
| 55 | }, |
| 56 | { |
| 57 | TagType: TagTypeGroup, |
| 58 | Permissions: "r-x", |
| 59 | }, |
| 60 | { |
| 61 | TagType: TagTypeOther, |
| 62 | Permissions: "---", |
| 63 | }, |
| 64 | }, |
| 65 | } |
| 66 | assert.Equal(t, "user::rwx,group::r-x,other::---", acl.String()) |
| 67 | } |
| 68 | |
| 69 | func TestACLParse_WithValidACL(t *testing.T) { |
| 70 | expected := ACL{ |
| 71 | Entries: []ACE{ |
| 72 | { |
| 73 | TagType: TagTypeUser, |
| 74 | Permissions: "rwx", |
| 75 | }, |
| 76 | { |
| 77 | TagType: TagTypeGroup, |
| 78 | Permissions: "r-x", |
| 79 | }, |
| 80 | { |
| 81 | TagType: TagTypeOther, |
| 82 | Permissions: "---", |
| 83 | }, |
| 84 | }, |
| 85 | } |
| 86 | acl, err := ParseACL("user::rwx,group::r-x,other::---") |
| 87 | assert.Nil(t, err, "Expected ACE to parse successfully") |
| 88 | assert.Equal(t, expected, acl) |
| 89 | } |
| 90 | |
| 91 | func TestACLParse_WithInvalidACL(t *testing.T) { |
| 92 | _, err := ParseACL("user:rwx,group::r-x,other::---") |
| 93 | assert.EqualError(t, err, "ACE string should have either 3 or 4 parts") |
| 94 | } |