Index: openacs-4/packages/chat/tcl/test/chat-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/chat/tcl/test/chat-test-procs.tcl,v diff -u -r1.1.2.2 -r1.1.2.3 --- openacs-4/packages/chat/tcl/test/chat-test-procs.tcl 23 Feb 2023 12:48:26 -0000 1.1.2.2 +++ openacs-4/packages/chat/tcl/test/chat-test-procs.tcl 23 Feb 2023 13:38:33 -0000 1.1.2.3 @@ -4,18 +4,45 @@ aa_register_case \ -procs { + "::chat::Package proc get_user_name" "::xo::db::chat_room instproc save_new" "::xo::db::chat_room instproc grant_creator" + "::xo::db::chat_room instproc grant_moderator" + "::xo::db::chat_room instproc revoke_moderator" + "::xo::db::chat_room instproc grant_user" + "::xo::db::chat_room instproc revoke_user" + "::xo::db::chat_room instproc ban_user" + "::xo::db::chat_room instproc unban_user" + "::xo::db::chat_room instproc count_messages" + "::xo::db::chat_room instproc post_message" + "::xo::db::chat_room instproc flush" + "::xo::db::chat_room instproc create_transcript" + "::xo::db::chat_room instproc transcript_messages" + "::xo::db::chat_transcript instproc save_new" + "::xo::db::chat_room instproc delete_messages" + "::xo::db::chat_room instproc delete" + "::chat::Package proc flush_rooms" } chat_room_tests { Test the chat room api } { set test_user [acs::test::user::create] + set test_user_2 [acs::test::user::create] aa_run_with_teardown -rollback -test_code { - set package_id [db_string get_package { + aa_section "Utilities" + + set user [acs_user::get -user_id [dict get $test_user user_id]] + aa_equals "get_user_name returns expected" \ + [chat::Package get_user_name -user_id [dict get $test_user user_id]] \ + [expr {[dict get $user screen_name] ne "" ? + [dict get $user screen_name] : [dict get $user name]}] + + + aa_section "Chat Room Creation" + set package_id [::xo::dc get_value get_package { select max(package_id) from apm_packages }] @@ -51,15 +78,149 @@ }] aa_true "Creation user was granted the expected privileges" { - [permission::permission_p -party_id $test_user_id -object_id $room_id -privilege chat_room_edit] && - [permission::permission_p -party_id $test_user_id -object_id $room_id -privilege chat_room_view] && - [permission::permission_p -party_id $test_user_id -object_id $room_id -privilege chat_room_delete] && - [permission::permission_p -party_id $test_user_id -object_id $room_id -privilege chat_transcript_create] + [permission::permission_p \ + -party_id $test_user_id -object_id $room_id -privilege chat_room_edit] && + [permission::permission_p \ + -party_id $test_user_id -object_id $room_id -privilege chat_room_view] && + [permission::permission_p \ + -party_id $test_user_id -object_id $room_id -privilege chat_room_delete] && + [permission::permission_p \ + -party_id $test_user_id -object_id $room_id -privilege chat_transcript_create] } + + aa_section "Permission api" + + set test_user_id_2 [dict get $test_user_2 user_id] + + aa_log "Grant moderator to user '$test_user_id_2'" + $r grant_moderator -party_id $test_user_id_2 + aa_true "User '$test_user_id_2' was granted the expected privilege" \ + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_room_moderate] + + aa_log "Revoke moderator to user '$test_user_id_2'" + $r revoke_moderator -party_id $test_user_id_2 + aa_false "User '$test_user_id_2' was revoked the expected privilege" \ + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_room_moderate] + + aa_log "Grant usage to user '$test_user_id_2'" + $r grant_user -party_id $test_user_id_2 + aa_true "User '$test_user_id_2' was granted the expected privilege" { + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_read] && + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_write] + } + + aa_log "Revoke usage to user '$test_user_id_2'" + $r revoke_user -party_id $test_user_id_2 + aa_false "User '$test_user_id_2' was revoked the expected privilege" { + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_read] && + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_write] + } + + aa_log "Ban user '$test_user_id_2'" + $r ban_user -party_id $test_user_id_2 + aa_true "User '$test_user_id_2' was banned" \ + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_ban] + + aa_log "Unban user '$test_user_id_2'" + $r unban_user -party_id $test_user_id_2 + aa_false "User '$test_user_id_2' was unbanned" \ + [permission::permission_p \ + -party_id $test_user_id_2 -object_id $room_id -privilege chat_ban] + + + aa_section "Messaging" + + aa_equals "Chat Room '$room_id' is empty" \ + [$r count_messages] 0 + + $r post_message \ + -creation_user $test_user_id_2 \ + -creation_ip 192.168.1.1 \ + -msg "Hello there!" + + $r post_message \ + -creation_user $test_user_id \ + -creation_ip 192.168.1.2 \ + -msg "General Kenobi!" + + aa_equals "Chat Room '$room_id' has 2 messages" \ + [$r count_messages] 2 + + aa_log "Flushing all messages" + $r flush + + set transcript [::xo::dc get_value get_transcript { + select contents from chat_transcripts + where room_id = :room_id + }] + aa_true "Transcript '$transcript' contains the expected content" { + [string first "Hello there!" $transcript] >= 0 && + [string first "General Kenobi!" $transcript] >= 0 && + [string first [chat::Package get_user_name -user_id $test_user_id] $transcript] >= 0 && + [string first [chat::Package get_user_name -user_id $test_user_id_2] $transcript] >= 0 + } + + aa_equals "Chat Room '$room_id' is empty again" \ + [$r count_messages] 0 + + + aa_section "Deleting room" + $r delete + aa_false "Room was deleted" [::xo::dc 0or1row check { + select 1 from chat_rooms where room_id = :room_id + }] + + + aa_section "Flushing all rooms" + + aa_log "Creating a few rooms, with a couple messages" + set other_rooms [list] + for {set i 2} {$i <= 4} {incr i} { + set r [::xo::db::chat_room new \ + -description "Test Chat Room $i" \ + -archive_p t \ + -auto_flush_p t \ + -auto_transcript_p t \ + -creation_user $test_user_id \ + -package_id $package_id \ + -pretty_name "Test Room $i"] + lappend other_rooms [$r save_new] + + $r post_message \ + -creation_user $test_user_id_2 \ + -creation_ip 192.168.1.1 \ + -msg "Ping!" + $r post_message \ + -creation_user $test_user_id \ + -creation_ip 192.168.1.2 \ + -msg "Pong!" + } + + aa_log "Flush the rooms" + ::chat::Package flush_rooms + + aa_equals "3 new transcripts have been created" \ + [::xo::dc get_value count \ + "select count(*) from chat_transcripts where room_id in ([join $other_rooms ,])"] \ + 3 + + aa_false "Rooms are empty now" \ + [::xo::dc 0or1row check_messages \ + "select 1 from chat_msgs where room_id in ([join $other_rooms ,])"] + } -teardown_code { acs::test::user::delete -user_id [dict get $test_user user_id] \ -delete_created_acs_objects + acs::test::user::delete -user_id [dict get $test_user_2 user_id] \ + -delete_created_acs_objects } }