From 0bbbe869aeef58294989cfe7f165fb59c01effb4 Mon Sep 17 00:00:00 2001
From: "Alexander \"Ace\" Olofsson" <ace@haxalot.com>
Date: Fri, 25 Aug 2023 08:56:33 +0200
Subject: [PATCH] Starting conversion to webmock

---
 Gemfile                      |  1 +
 test/api_cs_protocol_test.rb | 49 ++++++++++++++++++++++++++++++------
 test/test_helper.rb          |  1 +
 3 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/Gemfile b/Gemfile
index defdb2d..af35ca9 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
 gemspec
 
 # gem 'ci_reporter_test_unit'
+gem 'webmock'
diff --git a/test/api_cs_protocol_test.rb b/test/api_cs_protocol_test.rb
index 609e506..c776891 100644
--- a/test/api_cs_protocol_test.rb
+++ b/test/api_cs_protocol_test.rb
@@ -2,11 +2,7 @@ require 'test_helper'
 
 class ApiTest < Test::Unit::TestCase
   def setup
-    @http = mock
-    @http.stubs(:active?).returns(true)
-
     @api = MatrixSdk::Api.new 'https://example.com', protocols: :CS, threadsafe: false
-    @api.instance_variable_set :@http, @http
     @api.stubs(:print_http)
 
     matrixsdk_add_api_stub
@@ -20,19 +16,56 @@ class ApiTest < Test::Unit::TestCase
     response
   end
 
+  def stub_versions_request
+    stub_request(:get, '/_matrix/client/versions').to_return_json(
+      body: {
+        versions: [ "r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0", "r0.5.0", "r0.6.0","r0.6.1", "v1.1", "v1.2", "v1.3", "v1.4","v1.5","v1.6" ],
+        unstable_features: {
+          "org.matrix.label_based_filtering": true,
+          "org.matrix.e2e_cross_signing": true,
+          "org.matrix.msc2432": true,
+          "uk.half-shot.msc2666.query_mutual_rooms": true,
+          "io.element.e2ee_forced.public": false,
+          "io.element.e2ee_forced.private": false,
+          "io.element.e2ee_forced.trusted_private": false,
+          "org.matrix.msc3026.busy_presence": false,
+          "org.matrix.msc2285.stable": true,
+          "org.matrix.msc3827.stable": true,
+          "org.matrix.msc3440.stable": true,
+          "org.matrix.msc3771": true,
+          "org.matrix.msc3773": false,
+          "fi.mau.msc2815": false,
+          "fi.mau.msc2659.stable": true,
+          "org.matrix.msc3882": false,
+          "org.matrix.msc3881": false,
+          "org.matrix.msc3874": false,
+          "org.matrix.msc3886": false,
+          "org.matrix.msc3912": false,
+          "org.matrix.msc3981": false,
+          "org.matrix.msc3391": false
+        }
+      }
+    )
+  end
+
   def test_api_versions
-    @http.expects(:request).returns(mock_success('{"versions":["r0.3.0","r0.4.0"]}'))
+    stub_versions_request
     assert_equal 'r0.4.0', @api.client_api_versions.latest
   end
 
   def test_api_unsable_features
-    @http.expects(:request).returns(mock_success('{"unstable_features":{"lazy_loading_members": true}}'))
-    assert_equal true, @api.client_api_unstable_features.has?(:lazy_loading_members)
+    stub_versions_request
+    assert_equal true, @api.client_api_unstable_features.has?(:"org.matrix.label_based_filtering")
   end
 
   def test_whoami
-    @http.expects(:request).returns(mock_success('{"user_id":"@user:example.com"}'))
+    stub_get = stub_request(:get, '/_matrix/client/v3/account/whoami').to_return_json(
+      body: {
+      }
+    )
+
     assert_equal @api.whoami?, user_id: '@user:example.com'
+    assert_requested stub_get
   end
 
   def test_sync
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 45ca415..486fd45 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -9,6 +9,7 @@ require 'matrix_sdk'
 
 require 'test/unit'
 require 'mocha/test_unit'
+require 'webmock/test_unit'
 
 RUBY_MAJOR_MINOR_VERSION = RUBY_VERSION[0..2].freeze
 OLDER_RUBY = %w[2.5 2.6].include?(RUBY_MAJOR_MINOR_VERSION)
-- 
GitLab