@@ -86,3 +86,93 @@ pub(crate) async fn set_alias(
86
86
87
87
Ok ( pm:: SetAliasResponse { } )
88
88
}
89
+
90
+ #[ cfg( test) ]
91
+ mod test {
92
+ use super :: * ;
93
+ use crate :: app:: test:: * ;
94
+
95
+ #[ tokio:: test]
96
+ async fn set_alias ( ) {
97
+ let app = TestApp :: new ( ) . await ;
98
+
99
+ // Nonexisting entity
100
+ super :: set_alias (
101
+ & app,
102
+ pm:: SetAliasRequest {
103
+ entity_id : Some ( EntityId :: Uid ( 99999999 ) . into ( ) ) ,
104
+ entity_type : pb:: EntityType :: Node . into ( ) ,
105
+ new_alias : "new_alias" . to_string ( ) ,
106
+ } ,
107
+ )
108
+ . await
109
+ . unwrap_err ( ) ;
110
+
111
+ // Invalid entity_type / entity_id combination
112
+ super :: set_alias (
113
+ & app,
114
+ pm:: SetAliasRequest {
115
+ entity_id : Some ( EntityId :: Uid ( 101001 ) . into ( ) ) ,
116
+ entity_type : pb:: EntityType :: Target . into ( ) ,
117
+ new_alias : "new_alias" . to_string ( ) ,
118
+ } ,
119
+ )
120
+ . await
121
+ . unwrap_err ( ) ;
122
+
123
+ // Alias already in use
124
+ super :: set_alias (
125
+ & app,
126
+ pm:: SetAliasRequest {
127
+ entity_id : Some ( EntityId :: Alias ( "meta_node_1" . try_into ( ) . unwrap ( ) ) . into ( ) ) ,
128
+ entity_type : pb:: EntityType :: Node . into ( ) ,
129
+ new_alias : "meta_node_2" . to_string ( ) ,
130
+ } ,
131
+ )
132
+ . await
133
+ . unwrap_err ( ) ;
134
+
135
+ // Deny setting client aliases
136
+ super :: set_alias (
137
+ & app,
138
+ pm:: SetAliasRequest {
139
+ entity_id : Some (
140
+ EntityId :: LegacyID ( LegacyId {
141
+ node_type : NodeType :: Client ,
142
+ num_id : 1 ,
143
+ } )
144
+ . into ( ) ,
145
+ ) ,
146
+ entity_type : pb:: EntityType :: Node . into ( ) ,
147
+ new_alias : "new_alias" . to_string ( ) ,
148
+ } ,
149
+ )
150
+ . await
151
+ . unwrap_err ( ) ;
152
+
153
+ // Success
154
+ super :: set_alias (
155
+ & app,
156
+ pm:: SetAliasRequest {
157
+ entity_id : Some ( EntityId :: Uid ( 101001 ) . into ( ) ) ,
158
+ entity_type : pb:: EntityType :: Node . into ( ) ,
159
+ new_alias : "new_alias" . to_string ( ) ,
160
+ } ,
161
+ )
162
+ . await
163
+ . unwrap ( ) ;
164
+
165
+ assert ! ( app. has_sent_notification:: <Heartbeat >( & [
166
+ NodeType :: Meta ,
167
+ NodeType :: Storage ,
168
+ NodeType :: Client ,
169
+ ] ) ) ;
170
+
171
+ assert_eq_db ! (
172
+ app,
173
+ "SELECT alias FROM entities WHERE uid = ?1" ,
174
+ [ 101001 ] ,
175
+ "new_alias"
176
+ ) ;
177
+ }
178
+ }
0 commit comments