From 9d889db503795925473ba4203383a4c341f35a9a Mon Sep 17 00:00:00 2001
From: Nemo <commits@captnemo.in>
Date: Thu, 07 Mar 2024 12:44:06 +0530
Subject: [PATCH] update tests

---
 README.md                                      |  7 +++++++
 test/test_client.rb                            | 45 +++++++++++++++++++++++++++++++++++++++++----
 test/fixtures/collections.export_all.200.json  | 31 +++++++++++++++++++++++++++++++
 test/fixtures/fileOperations.redirect.302.json |  1 +
 4 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index 373eb4f..8e82734 100644
--- a/README.md
+++ a/README.md
@@ -28,6 +28,13 @@
 client = Outliner.new('https://knowledge.example.com')
 pp client.auth_info
 pp client.collections_list(offset: 0, limit: 10)
+# This works around a 302 redirect bug in httparty
+begin
+  r = @client.fileOperations__redirect({id: FILE_OPERATION_ID}, format: nil, no_follow: true)
+rescue HTTParty::RedirectionTooDeep => e
+  # Download this using response = HTTParty.get e.response.header['location'] if needed
+  pp e.response.header['location']
+end
 ```
 
 ### Import
diff --git a/test/test_client.rb b/test/test_client.rb
index f266dfb..1c60509 100644
--- a/test/test_client.rb
+++ a/test/test_client.rb
@@ -6,6 +6,7 @@
 class ClientTest < Minitest::Test
   TOKEN = "c4302eFAKE_TOKEN9b6e27bccb7"
   BASE_URI='https://kb.example.com'
+  FILE_OPERATION_ID = "08d5db26-bf43-4ec9-ac62-8769fd828e94"
   def setup
     ENV['OUTLINE_TOKEN'] = TOKEN
     @client = Outliner::Client.new BASE_URI
@@ -16,26 +17,54 @@
   end
 
   def test_auth_info_api
-    mock('auth.info', 'auth.info.200')
-    auth_info = @client.auth_info
-    assert_equal "https://kb.example.com", auth_info['data']['team']['url']
+    mock('auth.info')
+    r = @client.auth__info
+    assert_equal "https://kb.example.com", r['data']['team']['url']
   end
 
+  def test_export
+    mock('collections.export_all')
+    r = @client.collections__export_all
+    assert_equal FILE_OPERATION_ID, r['data']['fileOperation']['id']
+    assert_equal 200, r['status']
+    assert_equal true, r['ok']
+  end
+
+  def test_retrieve_file_operation
+    mock("fileOperations.redirect", {
+      id: FILE_OPERATION_ID
+    }, {
+      "X-Download-Options" => "noopen",
+      "X-Content-Type-Options" => "nosniff",
+      "Content-Type" => "text/plain; charset=utf-8",
+      "Content-Length" => "459",
+      "Location" => "https://s3.example.com/#{FILE_OPERATION_ID}"
+    }, 302)
+    begin
+    r = @client.fileOperations__redirect({id: FILE_OPERATION_ID}, format: nil, no_follow: true)
+    rescue HTTParty::RedirectionTooDeep => e
+      assert_equal "302", e.response.code
+      assert_equal "https://s3.example.com/#{FILE_OPERATION_ID}", e.response.header['location']
+    end
+
+  end
+
   private
 
   def read_fixture(file)
     File.read "test/fixtures/#{file}.json"
   end
 
-  def mock(method_name, fixture_file, params = {})
+  def mock(method_name, params = {}, response_headers = {}, status = 200)
     stub_request(:post, BASE_URI + "/api/" + method_name)
     .with(
-        body: params.merge({token: TOKEN}).to_json,
+        body: params.to_json,
         headers: {
             'Accept'=>'application/json',
             'User-Agent'=>"Outliner/#{Outliner::VERSION}",
-            'Content-Type'=> 'application/json'
+            'Content-Type'=> 'application/json',
+            "Authorization"=> "Bearer #{TOKEN}"
         }
-    ).to_return(body: read_fixture(fixture_file))
+    ).to_return(body: read_fixture(method_name + ".#{status}"), headers: response_headers, status: 302)
   end
-end+end
diff --git a/test/fixtures/collections.export_all.200.json b/test/fixtures/collections.export_all.200.json
new file mode 100644
index 0000000..e12b1b2 100644
--- /dev/null
+++ a/test/fixtures/collections.export_all.200.json
@@ -1,0 +1,31 @@
+{
+  "success": true,
+  "data": {
+    "fileOperation": {
+      "id": "08d5db26-bf43-4ec9-ac62-8769fd828e94",
+      "type": "export",
+      "format": "outline-markdown",
+      "name": "Acme-export.zip",
+      "state": "creating",
+      "error": null,
+      "size": "0",
+      "collectionId": null,
+      "user": {
+        "id": "817fb131-4a9b-4981-9002-38c2503adc3e",
+        "name": "Acme Admin",
+        "avatarUrl": "https://fake-avatar-url.com",
+        "color": "#2BC2FF",
+        "isAdmin": true,
+        "isSuspended": false,
+        "isViewer": false,
+        "createdAt": "2024-03-07T04:03:45.204Z",
+        "updatedAt": "2024-03-07T06:51:26.023Z",
+        "lastActiveAt": "2024-03-07T06:51:26.023Z"
+      },
+      "createdAt": "2024-03-07T06:51:26.031Z",
+      "updatedAt": "2024-03-07T06:51:26.031Z"
+    }
+  },
+  "status": 200,
+  "ok": true
+}
diff --git a/test/fixtures/fileOperations.redirect.302.json b/test/fixtures/fileOperations.redirect.302.json
new file mode 100644
index 0000000..b357bb3 100644
--- /dev/null
+++ a/test/fixtures/fileOperations.redirect.302.json
@@ -1,0 +1,1 @@
+Redirecting to https://fake.s3-accelerate.amazonaws.com/uploads/3e11b7f9-f1c0-44d0-a21b-4d6e0561e9c9/a5b6985a-cff6-4d03-be60-20c517bee63e/Acme-export.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXXXX%2F20240307%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240307T055812Z&X-Amz-Expires=60&X-Amz-Signature=ff759b27ddfd5c7401c1715411a8ceba886f9f462c9b52fc0c4a5906e99ecd22&X-Amz-SignedHeaders=host&response-content-disposition=attachment.--
rgit 0.1.5