discourse/plugins/chat/spec/system
Alan Guo Xiang Tan 6e9fbb5bab
DEV: Do not process requests initiated by browser in a different example (#25809)
Why this change?

We noticed that running `LOAD_PLUGINS=1 rspec --seed=38855 plugins/chat/spec/system/chat_new_message_spec.rb` locally
results in the system tests randomly failing. When we inspected the
request logs closely, we noticed that a `/presence/get` request from a
previous rspec example was being processed when a new rspec example is
already being run. We know it was from the previous rspec example
because inspecting the auth token showed the request using the auth
token of a user from the previous example. However, when a request using
an auth token from a previous example is used it ends up logging out the
same user on the server side because the user id in the cookie is the same
due to the use of `fab!`.

I did some research and there is apparently no way to wait until all
inflight requests by the browser has completed through capybara or
selenium. Therefore, we will add an identifier by attaching a cookie to all non-xhr requests so that
xhr requests which are triggered subsequently will contain the cookie in the request.

In the `BlockRequestsMiddleware` middleware, we will then reject any
requests when the value of the identifier in the cookie does not match the current rspec's example
location.

To see the problem locally, change `Auth::DefaultCurrentUserProvider.find_v1_auth_cookie` to the following:

```
  def self.find_v1_auth_cookie(env)
    return env[DECRYPTED_AUTH_COOKIE] if env.key?(DECRYPTED_AUTH_COOKIE)

    env[DECRYPTED_AUTH_COOKIE] = begin
      request = ActionDispatch::Request.new(env)
      cookie = request.cookies[TOKEN_COOKIE]

      # don't even initialize a cookie jar if we don't have a cookie at all
      if cookie&.valid_encoding? && cookie.present?
        puts "#{env["REQUEST_PATH"]} #{request.cookie_jar.encrypted[TOKEN_COOKIE]&.with_indifferent_access}"
        request.cookie_jar.encrypted[TOKEN_COOKIE]&.with_indifferent_access
      end
    end
  end
```

After which run the following command: `LOAD_PLUGINS=1 rspec --format documentation --seed=38855 plugins/chat/spec/system/chat_new_message_spec.rb`

It takes a few tries but the last spec should fail and you should see something like this:

```
assets/chunk.c16f6ba8b6824baa47ac.d41d8cd9.js {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/assets/chunk.050148142e1d2dc992dd.d41d8cd9.js {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/chat/api/channels/527/messages {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/uploads/default/test_0/optimized/1X/_129430568242d1b7f853bb13ebea28b3f6af4e7_2_512x512.png {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
    redirects to existing chat channel
    redirects to chat channel if recipients param is missing (PENDING: Temporarily skipped with xit)
  with multiple users
/favicon.ico {"token"=>"9a75c114c4d3401509a23d240f0a46d4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591736}
/chat/new-message {"token"=>"9a75c114c4d3401509a23d240f0a46d4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591736}
/presence/get {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
 ```
 
 Note how the `/presence/get` request is using a token from the previous example. 

Co-authored-by: David Taylor <david@taylorhq.com>
2024-02-22 19:41:10 +08:00
..
admin DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
chat/composer DEV: Remove unnecessary js: true options from specs (#24463) 2023-11-20 17:22:14 +01:00
chat_message PERF: cook message in background (#24227) 2023-11-06 15:45:30 +01:00
list_channels FIX: sort chat channels by slug (#25656) 2024-02-13 12:59:46 +08:00
page_objects FEATURE: Create a link to start a new chat (#25722) 2024-02-20 18:08:57 +08:00
reply_to_message DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
select_message FEATURE: thread pagination (#22624) 2023-07-27 09:57:03 +02:00
shortcuts DEV: Refactor chat specs related to message creation 2023-08-31 11:21:23 +02:00
thread_list DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
thread_tracking DEV: skip two flakey specs (#24965) 2023-12-19 09:27:09 +01:00
user_menu_notifications DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
user_status DEV: Remove assertion causing test to be flaky (#25069) 2023-12-29 13:23:16 +08:00
admin_sidebar_navigation_spec.rb FIX: Admin sidebar was hiding chat/forum toggle button (#25781) 2024-02-21 14:44:09 +10:00
anonymous_spec.rb DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
archive_channel_spec.rb PERF: cook message in background (#24227) 2023-11-06 15:45:30 +01:00
bookmark_message_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
browse_page_spec.rb FIX: set channels tab as default on mobile chat footer (#25296) 2024-01-17 17:12:55 +08:00
channel_members_page_spec.rb UX: Chat info area back button + styling tweaks (#24966) 2023-12-19 16:28:07 +01:00
channel_message_upload_spec.rb DEV: Fix various rubocop lints (#24749) 2023-12-06 23:25:00 +01:00
channel_settings_page_spec.rb UX: Chat browse redesign (#25698) 2024-02-15 18:00:22 +01:00
channel_thread_message_echoing_spec.rb DEV: makes every spec use new messages helper (#23163) 2023-08-21 16:31:58 +02:00
chat_channel_spec.rb DEV: removes flakey spec (#25405) 2024-01-24 18:56:50 +01:00
chat_composer_draft_spec.rb FEATURE: implements drafts for threads (#24483) 2023-11-22 11:54:23 +01:00
chat_composer_spec.rb DEV: Fix flaky network-based upload spec (#23286) 2023-08-28 12:59:22 +08:00
chat_footer_spec.rb FIX: Conditionally hide 'My Threads' on mobile (#25494) 2024-01-31 09:09:04 -03:00
chat_message_creator_spec.rb FEATURE: Allow users to DM groups in chat (#25189) 2024-01-19 11:09:47 -03:00
chat_message_onebox_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
chat_new_message_spec.rb DEV: Do not process requests initiated by browser in a different example (#25809) 2024-02-22 19:41:10 +08:00
chat_summarization_spec.rb DEV: Remove unnecessary js: true options from specs (#24463) 2023-11-20 17:22:14 +01:00
closed_channel_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
create_channel_spec.rb DEV: Remove experimental site setting for chat threads (#22720) 2023-07-26 12:46:23 +02:00
dates_separators_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
deleted_channel_spec.rb PERF: cook message in background (#24227) 2023-11-06 15:45:30 +01:00
deleted_message_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
document_title_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
drawer_spec.rb UX: chat channel title links to channel settings (#25785) 2024-02-21 17:53:04 +08:00
edited_message_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
flag_message_spec.rb UX: enhances chat copy features (#23770) 2023-10-04 16:14:37 +02:00
hashtag_autocomplete_spec.rb FEATURE: Async load of category and chat hashtags (#25526) 2024-02-12 12:07:14 +02:00
invite_users_to_channel_spec.rb FIX: ensures users can open channel invites (#24067) 2023-10-24 18:51:33 +02:00
kick_user_from_channel_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
mention_warnings_spec.rb FEATURE: Chat global mention warnings (pre-send & post-send) (#22764) 2023-08-22 15:54:35 -05:00
message_errors_spec.rb DEV: ensures we have error message before message check (#24647) 2023-11-30 10:55:05 +01:00
message_notifications_mobile_spec.rb FIX: chat channel row indicator should only show urgent count (#25458) 2024-01-31 16:47:54 +08:00
message_notifications_with_sidebar_spec.rb PERF: cook message in background (#24227) 2023-11-06 15:45:30 +01:00
message_thread_indicator_spec.rb DEV: improves reliability of delete/restore/update specs (#24265) 2023-11-07 11:34:35 +01:00
message_user_info_spec.rb DEV: Fix RSpec/SpecFilePathSuffix / enable 3 chat spec files (#24750) 2023-12-06 22:25:11 +01:00
move_message_to_channel_spec.rb DEV: makes chat modals use the new <DModal /> component (#22495) 2023-07-10 13:43:33 +02:00
navigation_spec.rb FIX: Save previous chat state when navigating with the sidebar (#25537) 2024-02-06 13:11:12 -03:00
react_to_message_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
read_only_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
removing_channel_spec.rb FIX: redirects to browse after removing last followed (#22563) 2023-07-14 08:26:18 +02:00
restore_message_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
reviewables_spec.rb FIX: 500 error when reviewable has a missing message (#25113) 2024-01-03 11:49:54 -03:00
send_message_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
separate_sidebar_mode_spec.rb FIX: Save previous chat state when navigating with the sidebar (#25537) 2024-02-06 13:11:12 -03:00
sidebar_navigation_menu_spec.rb DEV: correctly uses link to message endpoint in spec (#24652) 2023-11-30 19:33:41 +01:00
sidebars_spec.rb DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
silenced_user_spec.rb DEV: Remove superfluous js: true metadata (#21960) 2023-06-07 09:26:58 +08:00
single_thread_spec.rb DEV: Do not process requests initiated by browser in a different example (#25809) 2024-02-22 19:41:10 +08:00
thread_preview_spec.rb DEV: improves reliability of delete/restore/update specs (#24265) 2023-11-07 11:34:35 +01:00
transcript_spec.rb DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
unfollow_dm_channel_spec.rb DEV: Remove the use of Capybara::Session#quit (#24978) 2023-12-20 13:20:14 +08:00
update_last_read_spec.rb DEV: Fix RSpec/SpecFilePathSuffix / enable 3 chat spec files (#24750) 2023-12-06 22:25:11 +01:00
uploads_spec.rb DEV: skip flaky upload spec for large image thumbnails (#24745) 2023-12-06 19:08:56 +08:00
user_card_spec.rb DEV: Fix random typos (#24756) 2023-12-06 22:25:26 +01:00
user_chat_preferences_spec.rb FIX: correctly check chat tab is present (#23200) 2023-08-23 13:06:29 +02:00
user_threads_spec.rb FIX: Hide 'My Threads' if no followed channels have threads (#25470) 2024-01-30 10:53:32 -03:00
visit_channel_spec.rb DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00