11use std:: fs:: OpenOptions ;
2- use std:: io;
32use std:: io:: Write ;
43
54use marine_rs_sdk:: marine;
@@ -24,17 +23,19 @@ pub fn set_script_source_to_file(script: String) -> UnitValue {
2423 return UnitValue :: error ( "Only owner of the service can set the script" ) ;
2524 }
2625
26+ // open file for writing, overwrite if exists, create if not exists
2727 let write = OpenOptions :: new ( )
28- . create_new ( true )
28+ // create file if it doesn't exist
29+ . create ( true )
30+ // remove all contents of the file if it exists
31+ . truncate ( true )
32+ // grant writing permissions
2933 . write ( true )
3034 . open ( SCRIPT_FILE )
3135 . map ( |mut f| f. write_all ( script. as_bytes ( ) ) ) ;
3236
3337 match write {
3438 Ok ( _) => UnitValue :: ok ( ) ,
35- Err ( e) if e. kind ( ) == io:: ErrorKind :: AlreadyExists => {
36- UnitValue :: error ( "Script can be set only once, and it was already set" )
37- }
3839 Err ( e) => UnitValue :: error ( format ! ( "Error writing script to {}: {}" , SCRIPT_FILE , e) ) ,
3940 }
4041}
@@ -100,8 +101,8 @@ pub fn script_cid() -> CIDValue {
100101#[ test_env_helpers:: after_each]
101102#[ cfg( test) ]
102103mod tests {
103- use marine_rs_sdk_test:: marine_test;
104104 use marine_rs_sdk_test:: CallParameters ;
105+ use marine_rs_sdk_test:: marine_test;
105106
106107 use crate :: schema:: DB_FILE ;
107108
@@ -143,14 +144,52 @@ mod tests {
143144 ) ;
144145 let second_set = spell. set_script_source_to_file ( "(seq (null) (null))" . to_string ( ) ) ;
145146 assert ! (
146- !second_set. success,
147- "set_script_source_to_file returned true expected false"
148- ) ;
149- assert_eq ! (
150- second_set. error,
151- "Script can be set only once, and it was already set"
147+ second_set. success,
148+ "set_script_source_to_file returned false (fail), expected true (success)"
152149 ) ;
153- assert_eq ! ( spell. get_script_source_from_file( ) . source_code, "(null)" ) ;
150+ assert_eq ! ( spell. get_script_source_from_file( ) . source_code, "(seq (null) (null))" ) ;
151+ }
152+
153+ #[ marine_test(
154+ config_path = "../tests_artifacts/Config.toml" ,
155+ modules_dir = "../tests_artifacts"
156+ ) ]
157+ fn test_set_script_by_spell ( spell : marine_test_env:: spell:: ModuleInterface ) {
158+ let service_id = uuid:: Uuid :: new_v4 ( ) ;
159+ let particle_id = format ! ( "spell_{}_123" , service_id) ;
160+
161+ let cp = CallParameters {
162+ init_peer_id : "folex" . to_string ( ) ,
163+ service_creator_peer_id : "folex" . to_string ( ) ,
164+ service_id : service_id. to_string ( ) ,
165+ host_id : "" . to_string ( ) ,
166+ particle_id : particle_id,
167+ tetraplets : vec ! [ ] ,
168+ } ;
169+
170+ let set = spell. set_script_source_to_file_cp ( "(null)" . to_string ( ) , cp) ;
171+
172+ assert ! ( set. success, "set script failed: {}" , set. error) ;
173+ }
174+
175+ #[ marine_test(
176+ config_path = "../tests_artifacts/Config.toml" ,
177+ modules_dir = "../tests_artifacts"
178+ ) ]
179+ fn test_set_script_by_third_party ( spell : marine_test_env:: spell:: ModuleInterface ) {
180+ let cp = CallParameters {
181+ init_peer_id : "definitely not folex" . to_string ( ) ,
182+ service_creator_peer_id : "folex" . to_string ( ) ,
183+ service_id : "spell_service_id" . to_string ( ) ,
184+ host_id : "" . to_string ( ) ,
185+ particle_id : "some_particle_id_from_somewhere" . to_string ( ) ,
186+ tetraplets : vec ! [ ] ,
187+ } ;
188+
189+ let set = spell. set_script_source_to_file_cp ( "(null)" . to_string ( ) , cp) ;
190+
191+ assert ! ( !set. success, "set script succeeded while shouldn't" ) ;
192+ assert_eq ! ( set. error, "Only owner of the service can set the script" ) ;
154193 }
155194
156195 #[ marine_test(
0 commit comments