@@ -5,29 +5,175 @@ import (
5
5
"testing"
6
6
)
7
7
8
- // TestRestrictionsIdentity is a basic test that checks that the identity function returns the same value as the input.
9
- func TestRestrictionsIdentity (t * testing.T ) {
10
- //Test data
11
- input := Schema {
12
- Tables : []Table {
13
- {
14
- Name : "test" ,
15
- Columns : []Column {
16
- {
17
- Name : "test" ,
8
+ var example_schema = Schema {
9
+ Tables : []Table {
10
+ {
11
+ Name : "users" ,
12
+ Columns : []Column {
13
+ {
14
+ Name : "id" ,
15
+ Type : "int" ,
16
+ Nullable : false ,
17
+ Key : "PRIMARY KEY" ,
18
+ Extra : "AUTO_INCREMENT" ,
19
+ },
20
+ {
21
+ Name : "username" ,
22
+ Type : "varchar(255)" ,
23
+ Nullable : false ,
24
+ Key : "UNIQUE KEY" ,
25
+ Extra : "" ,
26
+ },
27
+ {
28
+ Name : "email" ,
29
+ Type : "varchar(255)" ,
30
+ Nullable : false ,
31
+ Key : "UNIQUE KEY" ,
32
+ Extra : "" ,
33
+ },
34
+ },
35
+ },
36
+ {
37
+ Name : "posts" ,
38
+ Columns : []Column {
39
+ {
40
+ Name : "id" ,
41
+ Type : "int" ,
42
+ Nullable : false ,
43
+ Key : "PRIMARY KEY" ,
44
+ Extra : "AUTO_INCREMENT" ,
45
+ },
46
+ {
47
+ Name : "title" ,
48
+ Type : "varchar(255)" ,
49
+ Nullable : false ,
50
+ Key : "" ,
51
+ Extra : "" ,
52
+ },
53
+ {
54
+ Name : "content" ,
55
+ Type : "text" ,
56
+ Nullable : true ,
57
+ Key : "" ,
58
+ Extra : "" ,
59
+ },
60
+ {
61
+ Name : "user_id" ,
62
+ Type : "int" ,
63
+ Nullable : false ,
64
+ Key : "FOREIGN KEY" ,
65
+ Extra : "" ,
66
+ Reference : & Reference {
67
+ Table : "users" ,
68
+ Column : "id" ,
69
+ OnDelete : "CASCADE" ,
70
+ OnUpdate : "CASCADE" ,
18
71
},
19
72
},
20
73
},
21
74
},
75
+ {
76
+ Name : "meta" ,
77
+ Columns : []Column {
78
+ {
79
+ Name : "meta_key" ,
80
+ Type : "varchar(255)" ,
81
+ Nullable : false ,
82
+ Key : "" ,
83
+ Extra : "" ,
84
+ },
85
+ {
86
+ Name : "meta_value" ,
87
+ Type : "varchar(255)" ,
88
+ Nullable : false ,
89
+ Key : "" ,
90
+ Extra : "" ,
91
+ },
92
+ },
93
+ },
94
+ },
95
+ }
96
+
97
+ func get_table_names (_input Schema ) []string {
98
+ var output []string
99
+ for _ , table := range _input .Tables {
100
+ output = append (output , table .Name )
101
+ }
102
+ return output
103
+ }
104
+
105
+ func get_column_names (_input Table ) []string {
106
+ var output []string
107
+ for _ , column := range _input .Columns {
108
+ output = append (output , column .Name )
22
109
}
23
- expected := input
110
+ return output
111
+ }
24
112
113
+ // TestRestrictionsIdentity is a basic test that checks that the identity function returns the same value as the input.
114
+ func TestRestrictionsIdentity (t * testing.T ) {
25
115
//Execute test
26
- actual := Restrict (input , Standard )
116
+ actual := Restrict (example_schema , Standard )
27
117
28
118
//Compare actual to expected
29
- if ! reflect .DeepEqual (actual , expected ) {
119
+ if ! reflect .DeepEqual (actual , example_schema ) {
30
120
t .Log ("Identity function failed to return the same value as the input." )
31
121
t .Fail ()
32
122
}
33
123
}
124
+
125
+ func TestRestrictionsMinimalist (t * testing.T ) {
126
+ actual := Restrict (example_schema , Minimalist )
127
+ names := get_table_names (actual )
128
+ if ! (equal_set (names , []string {"users" , "posts" })) {
129
+ t .Log ("Minimalist cleared inappropriate tables." )
130
+ t .Fail ()
131
+ }
132
+
133
+ if ! (equal_set (get_column_names (actual .Tables [0 ]), []string {"id" , "username" , "email" })) {
134
+ t .Log ("Minimalist cleared columns in a table where all the columns are keys." )
135
+ t .Fail ()
136
+ }
137
+
138
+ if ! (equal_set (get_column_names (actual .Tables [1 ]), []string {"id" , "user_id" })) {
139
+ t .Log ("Minimalist didn't clear the right columns in a table which has partial keys." )
140
+ t .Fail ()
141
+ }
142
+
143
+ }
144
+
145
+ // Simple example of a restriction function. This is intended to simulate the scenario where a user has been
146
+ // restricted to not be able to see the PII (Personally Identifiable Information) of other users. But they can
147
+ // still access the user_id fields for analysis purposes.
148
+ func example_permission_restrictor_analyst (_table Table , _column Column ) bool {
149
+ return _column .Name == "username" || _column .Name == "email"
150
+ }
151
+
152
+ // Another example of a restriction function. This is intended to simulate the scenario where a user has been
153
+ // restricted to not be able to see the content generated by users, but can still edit the metadata associated
154
+ // with the a user. This could be an example permission profile for a client account for the user management service.
155
+ func example_permission_restrictor_user_profile (_table Table , _column Column ) bool {
156
+ return _table .Name == "posts"
157
+ }
158
+
159
+ func TestRestrictionsCustom (t * testing.T ) {
160
+ analyst := Restrict (example_schema , example_permission_restrictor_analyst )
161
+ if ! (equal_set (get_table_names (analyst ), []string {"users" , "posts" , "meta" })) {
162
+ t .Log ("Custom cleared inappropriate tables." )
163
+ t .Fail ()
164
+ }
165
+
166
+ if ! (equal_set (get_column_names (analyst .Tables [0 ]), []string {"id" })) {
167
+ t .Log ("Custom failed to clear the PII columns in the users table." )
168
+ t .Fail ()
169
+ }
170
+
171
+ profile_service := Restrict (example_schema , example_permission_restrictor_user_profile )
172
+
173
+ if ! (equal_set (get_table_names (profile_service ), []string {"users" ,"meta" })) {
174
+ t .Log ("Custom cleared inappropriate tables." )
175
+ t .Fail ()
176
+ }
177
+
178
+
179
+ }
0 commit comments