github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/models/role_test.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package models 20 21 import ( 22 "context" 23 "strings" 24 "testing" 25 26 "github.com/e154/smart-home/adaptors" 27 m "github.com/e154/smart-home/models" 28 "github.com/e154/smart-home/system/access_list" 29 "github.com/e154/smart-home/system/migrations" 30 . "github.com/smartystreets/goconvey/convey" 31 ) 32 33 func TestRole(t *testing.T) { 34 35 Convey("add role", t, func(ctx C) { 36 _ = container.Invoke(func(adaptors *adaptors.Adaptors, 37 migrations *migrations.Migrations, 38 accessList access_list.AccessListService) { 39 40 // clear database 41 _ = migrations.Purge() 42 43 demoRole := &m.Role{ 44 Name: "demo", 45 } 46 err := adaptors.Role.Add(context.Background(), demoRole) 47 So(err, ShouldBeNil) 48 49 userRole := &m.Role{ 50 Name: "user", 51 Parent: demoRole, 52 } 53 err = adaptors.Role.Add(context.Background(), userRole) 54 So(err, ShouldBeNil) 55 56 //debug.Println(accessList.List) 57 //fmt.Println("----") 58 59 var counter int 60 for pack, item := range *accessList.List(context.Background()) { 61 for right := range item { 62 if strings.Contains(right, "read") || 63 strings.Contains(right, "view") { 64 permission := &m.Permission{ 65 RoleName: demoRole.Name, 66 PackageName: pack, 67 LevelName: right, 68 } 69 70 counter++ 71 _, err = adaptors.Permission.Add(context.Background(), permission) 72 So(err, ShouldBeNil) 73 } 74 } 75 } 76 77 for pack, item := range *accessList.List(context.Background()) { 78 for right := range item { 79 if !strings.Contains(right, "read") && 80 !strings.Contains(right, "view") { 81 permission := &m.Permission{ 82 RoleName: userRole.Name, 83 PackageName: pack, 84 LevelName: right, 85 } 86 87 counter++ 88 _, err = adaptors.Permission.Add(context.Background(), permission) 89 So(err, ShouldBeNil) 90 } 91 } 92 } 93 94 adminRole := &m.Role{ 95 Name: "admin", 96 Parent: userRole, 97 } 98 err = adaptors.Role.Add(context.Background(), adminRole) 99 So(err, ShouldBeNil) 100 101 // user 102 userRole, err = adaptors.Role.GetByName(context.Background(), "user") 103 So(err, ShouldBeNil) 104 So(userRole.Name, ShouldEqual, "user") 105 So(userRole.Parent, ShouldNotBeNil) 106 So(userRole.Parent.Name, ShouldEqual, "demo") 107 So(len(userRole.Children), ShouldEqual, 1) 108 So(userRole.Children[0].Name, ShouldEqual, "admin") 109 110 // demo 111 demoRole, err = adaptors.Role.GetByName(context.Background(), "demo") 112 So(err, ShouldBeNil) 113 So(demoRole.Parent, ShouldBeNil) 114 So(demoRole.Name, ShouldEqual, "demo") 115 So(len(demoRole.Children), ShouldEqual, 1) 116 So(demoRole.Children[0].Name, ShouldEqual, "user") 117 118 // admin 119 adminRole, err = adaptors.Role.GetByName(context.Background(), "admin") 120 So(err, ShouldBeNil) 121 So(adminRole.Parent, ShouldNotBeNil) 122 So(adminRole.Parent.Name, ShouldEqual, "user") 123 So(adminRole.Name, ShouldEqual, "admin") 124 So(len(adminRole.Children), ShouldBeZeroValue) 125 126 permissions, err := adaptors.Permission.GetAllPermissions(context.Background(), "user") 127 So(err, ShouldBeNil) 128 129 So(len(permissions), ShouldEqual, counter) 130 131 err = adaptors.Role.Delete(context.Background(), "demo") 132 So(err, ShouldNotBeNil) 133 err = adaptors.Role.Delete(context.Background(), "user") 134 So(err, ShouldNotBeNil) 135 err = adaptors.Role.Delete(context.Background(), "admin") 136 So(err, ShouldBeNil) 137 err = adaptors.Role.Delete(context.Background(), "user") 138 So(err, ShouldBeNil) 139 err = adaptors.Role.Delete(context.Background(), "demo") 140 So(err, ShouldBeNil) 141 }) 142 }) 143 }