|
| 1 | +package boxutil |
| 2 | + |
| 3 | +import ( |
| 4 | + . "box" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestTree(t *testing.T) { |
| 11 | + |
| 12 | + root := NewResource() |
| 13 | + v1 := root.Resource("/api/v1"). |
| 14 | + WithInterceptors(Auth, AccessLog) |
| 15 | + v1.Resource("/users"). |
| 16 | + WithActions( |
| 17 | + Get(ListUsers).WithInterceptors(AdminRequired, NoAudit), |
| 18 | + Post(CreateUser).WithInterceptors(AdminRequired), |
| 19 | + ) |
| 20 | + v1.Resource("/users/{userId}"). |
| 21 | + WithActions( |
| 22 | + Get(GetUser), |
| 23 | + Delete(DeleteUser), |
| 24 | + ActionPost(BanUser).WithInterceptors(AdminRequired), |
| 25 | + ActionPost(EnableUser).WithInterceptors(AdminRequired), |
| 26 | + ActionPost(DisableUser).WithInterceptors(AdminRequired), |
| 27 | + ) |
| 28 | + v1.Resource("/spots/{spotId}/menus/{menuId}"). |
| 29 | + WithActions( |
| 30 | + Delete(DeleteMenu).WithInterceptors(AdminRequired, NoAudit), |
| 31 | + Patch(UpdateMenu), |
| 32 | + ) |
| 33 | + |
| 34 | + s := Tree(root) |
| 35 | + |
| 36 | + expected := `/api/v1 |
| 37 | + <Auth> |
| 38 | + <AccessLog> |
| 39 | +/api/v1/users |
| 40 | + GET <AdminRequired><NoAudit> |
| 41 | + POST <AdminRequired> |
| 42 | +/api/v1/users/{userId} |
| 43 | + DELETE |
| 44 | + GET |
| 45 | + POST :banUser <AdminRequired> |
| 46 | + POST :disableUser <AdminRequired> |
| 47 | + POST :enableUser <AdminRequired> |
| 48 | +/api/v1/spots/{spotId}/menus/{menuId} |
| 49 | + DELETE <AdminRequired><NoAudit> |
| 50 | + PATCH |
| 51 | +` |
| 52 | + |
| 53 | + if s != expected { |
| 54 | + t.Errorf("Tree output does not match\nExpected: %s\nObtained:%s\n", expected, s) |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +func CreateUser() {} |
| 60 | +func GetUser() {} |
| 61 | +func BanUser() {} |
| 62 | +func DisableUser() {} |
| 63 | +func EnableUser() {} |
| 64 | +func DeleteUser() {} |
| 65 | +func ListUsers() {} |
| 66 | +func DeleteMenu() {} |
| 67 | +func UpdateMenu() {} |
| 68 | + |
| 69 | +func Auth(next H) H { |
| 70 | + return func(ctx context.Context) { |
| 71 | + next(ctx) |
| 72 | + } |
| 73 | +} |
| 74 | +func AccessLog(next H) H { |
| 75 | + return func(ctx context.Context) { |
| 76 | + next(ctx) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func AdminRequired(next H) H { |
| 81 | + return func(ctx context.Context) { |
| 82 | + next(ctx) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func NoAudit(next H) H { |
| 87 | + return func(ctx context.Context) { |
| 88 | + next(ctx) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestSortActions(t *testing.T) { |
| 93 | + |
| 94 | + actions := []*A{ |
| 95 | + {Name: "D", Bound: false}, |
| 96 | + {Name: "C", Bound: false}, |
| 97 | + {Name: "B", Bound: true, HttpMethod: "B"}, |
| 98 | + {Name: "A", Bound: true, HttpMethod: "A"}, |
| 99 | + } |
| 100 | + |
| 101 | + fmt.Println(actions) |
| 102 | + sortActions(actions) |
| 103 | + fmt.Println(actions) |
| 104 | + |
| 105 | + if actions[0].Name != "A" { |
| 106 | + t.Errorf("A should be action[0]") |
| 107 | + } |
| 108 | + if actions[1].Name != "B" { |
| 109 | + t.Errorf("B should be action[1]") |
| 110 | + } |
| 111 | + if actions[2].Name != "C" { |
| 112 | + t.Errorf("C should be action[2]") |
| 113 | + } |
| 114 | + if actions[3].Name != "D" { |
| 115 | + t.Errorf("D should be action[3]") |
| 116 | + } |
| 117 | +} |
0 commit comments