From 26e5464350e3ec27879fa8014811976600032810 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 23 Feb 2022 19:54:28 +0100
Subject: [PATCH] update test

---
 ...t_test_should_update__an_existing_file.yml | 85 +++++++++++++++++++
 .../communication/website/git_files.yml       |  5 ++
 test/fixtures/communication/website/posts.yml |  8 ++
 .../communication/website/git_file_test.rb    | 16 +++-
 4 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 test/cassettes/Communication_Website_GitFileTest_test_should_update__an_existing_file.yml

diff --git a/test/cassettes/Communication_Website_GitFileTest_test_should_update__an_existing_file.yml b/test/cassettes/Communication_Website_GitFileTest_test_should_update__an_existing_file.yml
new file mode 100644
index 000000000..b74060061
--- /dev/null
+++ b/test/cassettes/Communication_Website_GitFileTest_test_should_update__an_existing_file.yml
@@ -0,0 +1,85 @@
+---
+http_interactions:
+- request:
+    method: get
+    uri: https://api.github.com/repos/<TEST_GITHUB_REPOSITORY>/contents/content/posts/2010/2010-11-28-test-2.html
+    body:
+      encoding: US-ASCII
+      string: ''
+    headers:
+      Accept:
+      - application/vnd.github.v3+json
+      User-Agent:
+      - Octokit Ruby Gem 4.22.0
+      Content-Type:
+      - application/json
+      Authorization:
+      - token <TEST_GITHUB_TOKEN>
+      Accept-Encoding:
+      - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
+  response:
+    status:
+      code: 200
+      message: OK
+    headers:
+      Server:
+      - GitHub.com
+      Date:
+      - Wed, 23 Feb 2022 18:49:12 GMT
+      Content-Type:
+      - application/json; charset=utf-8
+      Transfer-Encoding:
+      - chunked
+      Cache-Control:
+      - private, max-age=60, s-maxage=60
+      Vary:
+      - Accept, Authorization, Cookie, X-GitHub-OTP
+      - Accept-Encoding, Accept, X-Requested-With
+      Etag:
+      - W/"8b58a401710ed6560d7d06353c0fdf46f587128f"
+      Last-Modified:
+      - Wed, 23 Feb 2022 18:16:13 GMT
+      X-Oauth-Scopes:
+      - repo
+      X-Accepted-Oauth-Scopes:
+      - ''
+      Github-Authentication-Token-Expiration:
+      - 2022-03-02 10:41:25 UTC
+      X-Github-Media-Type:
+      - github.v3; format=json
+      X-Ratelimit-Limit:
+      - '5000'
+      X-Ratelimit-Remaining:
+      - '4956'
+      X-Ratelimit-Reset:
+      - '1645643208'
+      X-Ratelimit-Used:
+      - '44'
+      X-Ratelimit-Resource:
+      - core
+      Access-Control-Expose-Headers:
+      - ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
+        X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes,
+        X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO,
+        X-GitHub-Request-Id, Deprecation, Sunset
+      Access-Control-Allow-Origin:
+      - "*"
+      Strict-Transport-Security:
+      - max-age=31536000; includeSubdomains; preload
+      X-Frame-Options:
+      - deny
+      X-Content-Type-Options:
+      - nosniff
+      X-Xss-Protection:
+      - '0'
+      Referrer-Policy:
+      - origin-when-cross-origin, strict-origin-when-cross-origin
+      Content-Security-Policy:
+      - default-src 'none'
+      X-Github-Request-Id:
+      - C988:DD33:741334:7734EF:621681A8
+    body:
+      encoding: ASCII-8BIT
+      string: '{"name":"2010-11-28-test-2.html","path":"content/posts/2010/2010-11-28-test-2.html","sha":"8b58a401710ed6560d7d06353c0fdf46f587128f","size":92,"url":"https://api.github.com/repos/<TEST_GITHUB_REPOSITORY>/contents/content/posts/2010/2010-11-28-test-2.html?ref=main","html_url":"https://github.com/<TEST_GITHUB_REPOSITORY>/blob/main/content/posts/2010/2010-11-28-test-2.html","git_url":"https://api.github.com/repos/<TEST_GITHUB_REPOSITORY>/git/blobs/8b58a401710ed6560d7d06353c0fdf46f587128f","download_url":"https://raw.githubusercontent.com/<TEST_GITHUB_REPOSITORY>/main/content/posts/2010/2010-11-28-test-2.html","type":"file","content":"LS0tCnRpdGxlOiAiVGVzdCAyIgpkYXRlOiAiMjAxMC0xMS0yOFQwMTowMDow\nMCswMTowMCIKc2x1ZzogInRlc3QtMiIKZGVzY3JpcHRpb246ID4KICAKLS0t\nCgo=\n","encoding":"base64","_links":{"self":"https://api.github.com/repos/<TEST_GITHUB_REPOSITORY>/contents/content/posts/2010/2010-11-28-test-2.html?ref=main","git":"https://api.github.com/repos/<TEST_GITHUB_REPOSITORY>/git/blobs/8b58a401710ed6560d7d06353c0fdf46f587128f","html":"https://github.com/<TEST_GITHUB_REPOSITORY>/blob/main/content/posts/2010/2010-11-28-test-2.html"}}'
+  recorded_at: Wed, 23 Feb 2022 18:49:12 GMT
+recorded_with: VCR 6.0.0
diff --git a/test/fixtures/communication/website/git_files.yml b/test/fixtures/communication/website/git_files.yml
index 953b55052..21a1d16a6 100644
--- a/test/fixtures/communication/website/git_files.yml
+++ b/test/fixtures/communication/website/git_files.yml
@@ -23,3 +23,8 @@
 git_file_1:
   about: test_post (Communication::Website::Post)
   website: website_with_github
+git_file_2:
+  about: test_post_2 (Communication::Website::Post)
+  website: website_with_github
+  previous_sha: 8b58a401710ed6560d7d06353c0fdf46f587128f
+  previous_path: content/posts/2010/2010-11-28-test-2.html
diff --git a/test/fixtures/communication/website/posts.yml b/test/fixtures/communication/website/posts.yml
index c2154ce20..aa5498d02 100644
--- a/test/fixtures/communication/website/posts.yml
+++ b/test/fixtures/communication/website/posts.yml
@@ -38,3 +38,11 @@ test_post:
   slug: test
   published: true
   published_at: 2010-11-28 00:00:00
+test_post_2:
+  university: default_university
+  website: website_with_github
+  title: Test 2
+  slug: test-2
+  published: true
+  published_at: 2010-11-28 00:00:00
+  text: new content
diff --git a/test/models/communication/website/git_file_test.rb b/test/models/communication/website/git_file_test.rb
index 8509604b6..9bfc4adce 100644
--- a/test/models/communication/website/git_file_test.rb
+++ b/test/models/communication/website/git_file_test.rb
@@ -23,10 +23,24 @@
 require "test_helper"
 
 class Communication::Website::GitFileTest < ActiveSupport::TestCase
-  test "should_create? for a new file" do
+  test "should_create? a new file" do
     VCR.use_cassette(location) do
       file = communication_website_git_files(:git_file_1)
       assert file.should_create?
     end
   end
+
+  test "should_update? an existing file" do
+    VCR.use_cassette(location) do
+      # To create the file, i first did that:
+      # file = communication_website_git_files(:git_file_2)
+      # file.website.git_repository.add_git_file file
+      # file.website.git_repository.sync!
+      # byebug
+      # Then i got the sha and path, pasted it in the fixtures,
+      # changed the text so the content would need an update.
+      file = communication_website_git_files(:git_file_2)
+      assert file.should_update?
+    end
+  end
 end
-- 
GitLab