-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_object_data_test.go
108 lines (88 loc) · 5.21 KB
/
server_object_data_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package caskin_test
import (
"testing"
"github.com/awatercolorpen/caskin"
"github.com/awatercolorpen/caskin/example"
"github.com/awatercolorpen/caskin/playground"
"github.com/stretchr/testify/assert"
)
func TestServer_ObjectData_CreateCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 3}
assert.Equal(t, caskin.ErrNoWritePermission, service.CheckCreateObjectData(stage.Admin, stage.Domain, data1, "role"))
data1.ObjectID = 2
assert.Equal(t, caskin.ErrInValidObjectType, service.CheckCreateObjectData(stage.Admin, stage.Domain, data1, "role"))
assert.NoError(t, service.CheckCreateObjectData(stage.Admin, stage.Domain, data1, "test"))
data2 := &example.OneObjectData{Name: "object_data_2", ObjectID: 1}
assert.Equal(t, caskin.ErrNoWritePermission, service.CheckCreateObjectData(stage.Member, stage.Domain, data2, caskin.ObjectTypeRole))
assert.NoError(t, service.CheckCreateObjectData(stage.Admin, stage.Domain, data2, caskin.ObjectTypeRole))
}
func TestServer_ObjectData_RecoverCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 2}
assert.Equal(t, caskin.ErrNotExists, service.CheckRecoverObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, stage.DB.Create(data1).Error)
assert.Equal(t, caskin.ErrAlreadyExists, service.CheckRecoverObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, stage.DB.Delete(data1, data1.GetID()).Error)
assert.NoError(t, service.CheckRecoverObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, service.CheckRecoverObjectData(stage.Member, stage.Domain, data1))
}
func TestServer_ObjectData_DeleteCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 2}
assert.Equal(t, caskin.ErrEmptyID, service.CheckDeleteObjectData(stage.Admin, stage.Domain, data1))
data1.ID = 1
assert.Equal(t, caskin.ErrNotExists, service.CheckDeleteObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, stage.DB.Create(data1).Error)
data2 := &example.OneObjectData{ID: 1, ObjectID: 1}
assert.Equal(t, caskin.ErrNotExists, service.CheckDeleteObjectData(stage.Admin, stage.Domain, data2))
data2.ObjectID = 2
assert.NoError(t, service.CheckDeleteObjectData(stage.Admin, stage.Domain, data2))
assert.NoError(t, service.CheckDeleteObjectData(stage.Member, stage.Domain, data2))
}
func TestServer_ObjectData_UpdateCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 1}
assert.Equal(t, caskin.ErrEmptyID, service.CheckUpdateObjectData(stage.Admin, stage.Domain, data1, "role"))
data1.ID = 1
assert.Equal(t, caskin.ErrNotExists, service.CheckUpdateObjectData(stage.Admin, stage.Domain, data1, "role"))
assert.NoError(t, stage.DB.Create(data1).Error)
data2 := &example.OneObjectData{ID: 1, ObjectID: 2}
assert.Equal(t, caskin.ErrInValidObjectType, service.CheckUpdateObjectData(stage.Admin, stage.Domain, data2, "role"))
data2.ObjectID = 1
assert.Equal(t, caskin.ErrInValidObjectType, service.CheckUpdateObjectData(stage.Admin, stage.Domain, data2, "test"))
assert.Equal(t, caskin.ErrNoWritePermission, service.CheckUpdateObjectData(stage.Member, stage.Domain, data2, "role"))
assert.NoError(t, service.CheckUpdateObjectData(stage.Admin, stage.Domain, data2, "role"))
}
func TestServer_ObjectData_ModifyCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 1}
assert.Equal(t, caskin.ErrEmptyID, service.CheckModifyObjectData(stage.Admin, stage.Domain, data1))
data1.ID = 1
assert.Equal(t, caskin.ErrNotExists, service.CheckModifyObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, stage.DB.Create(data1).Error)
data2 := &example.OneObjectData{ID: 1, ObjectID: 2}
assert.Equal(t, caskin.ErrNotExists, service.CheckModifyObjectData(stage.Admin, stage.Domain, data2))
data2.ObjectID = 1
assert.Equal(t, caskin.ErrNoWritePermission, service.CheckModifyObjectData(stage.Member, stage.Domain, data2))
assert.NoError(t, service.CheckModifyObjectData(stage.Admin, stage.Domain, data2))
}
func TestServer_ObjectData_GetCheck(t *testing.T) {
stage, _ := playground.NewPlaygroundWithSqlitePath(t.TempDir())
service := stage.Service
data1 := &example.OneObjectData{Name: "object_data_1", ObjectID: 1}
assert.Equal(t, caskin.ErrEmptyID, service.CheckGetObjectData(stage.Admin, stage.Domain, data1))
data1.ID = 1
assert.Equal(t, caskin.ErrNotExists, service.CheckGetObjectData(stage.Admin, stage.Domain, data1))
assert.NoError(t, stage.DB.Create(data1).Error)
data2 := &example.OneObjectData{ID: 1, ObjectID: 2}
assert.Equal(t, caskin.ErrNotExists, service.CheckGetObjectData(stage.Admin, stage.Domain, data2))
data2.ObjectID = 1
assert.NoError(t, service.CheckGetObjectData(stage.Admin, stage.Domain, data2))
assert.NoError(t, service.CheckGetObjectData(stage.Member, stage.Domain, data2))
}