From 0561cc63809f974d6818034e7ba7b9fc8a214466 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 22 Feb 2022 19:38:11 +0100
Subject: [PATCH] wip tests

---
 app/models/communication/website/git_file.rb  | 34 ++++++++++++-------
 .../templates/partners/_static.html.erb       | 11 ++++--
 .../communication/website/git_files.yml       |  3 ++
 .../communication/website/git_file_test.rb    | 14 ++++++++
 4 files changed, 47 insertions(+), 15 deletions(-)
 create mode 100644 test/fixtures/communication/website/git_files.yml
 create mode 100644 test/models/communication/website/git_file_test.rb

diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index 4ade6aec3..219a46db1 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -33,12 +33,9 @@ class Communication::Website::GitFile < ApplicationRecord
     website.git_repository.add_git_file git_file
   end
 
-  def synchronized_with_git?
-    git_sha == previous_sha || git_sha == previous_sha256
-  end
-
   def should_create?
     !should_destroy? &&
+    !exists_on_git? &&
     (
       !synchronized_with_git? ||
       previous_path.nil? ||
@@ -49,7 +46,7 @@ class Communication::Website::GitFile < ApplicationRecord
   def should_update?
     !should_destroy? &&
     (
-      different_path || different_sha
+      different_path? || different_sha?
     )
   end
 
@@ -61,14 +58,6 @@ class Communication::Website::GitFile < ApplicationRecord
     @path ||= about.git_path(website)&.gsub(/\/+/, '/')
   end
 
-  def different_path
-    previous_path != path
-  end
-
-  def different_sha
-    previous_sha != sha && previous_sha != sha256
-  end
-
   def sha
     # Git SHA-1 is calculated from the String "blob <length>\x00<contents>"
     # Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html
@@ -92,6 +81,25 @@ class Communication::Website::GitFile < ApplicationRecord
 
   protected
 
+  def exists_on_git?
+    !git_sha.nil?
+  end
+
+  def synchronized_with_git?
+    exists_on_git? &&
+    (
+      git_sha == previous_sha || git_sha == previous_sha256
+    )
+  end
+
+  def different_path?
+    previous_path != path
+  end
+
+  def different_sha?
+    previous_sha != sha && previous_sha != sha256
+  end
+
   def git_sha
     @git_sha ||= website.git_repository.git_sha previous_path
   end
diff --git a/app/views/admin/communication/website/blocks/templates/partners/_static.html.erb b/app/views/admin/communication/website/blocks/templates/partners/_static.html.erb
index 34559a6b2..ef381ee8d 100644
--- a/app/views/admin/communication/website/blocks/templates/partners/_static.html.erb
+++ b/app/views/admin/communication/website/blocks/templates/partners/_static.html.erb
@@ -5,11 +5,18 @@
         partners:
 <%
 element['partners'].each do |partner|
+  logo_id = nil
+  if partner.has_key?('logo')
+    logo = partner['logo']
+    if logo.is_a?(Hash) && logo.has_key?('id')
+      logo_id = logo['id']
+    end
+  end
 %>
           - name: "<%= partner['name'] %>"
             url: "<%= partner['url'] %>"
-            <% unless partner.dig('logo', 'id').blank? %>
-            logo: "<%= partner['logo']['id'] %>"
+            <% if logo_id %>
+            logo: "<%= logo_id %>"
             <% end %>
 <% end %>
 <% end %>
diff --git a/test/fixtures/communication/website/git_files.yml b/test/fixtures/communication/website/git_files.yml
new file mode 100644
index 000000000..c9b6c57aa
--- /dev/null
+++ b/test/fixtures/communication/website/git_files.yml
@@ -0,0 +1,3 @@
+git_file_1:
+  about: test_post (Communication::Website::Post)
+  website: website_with_github
diff --git a/test/models/communication/website/git_file_test.rb b/test/models/communication/website/git_file_test.rb
new file mode 100644
index 000000000..bbde17e00
--- /dev/null
+++ b/test/models/communication/website/git_file_test.rb
@@ -0,0 +1,14 @@
+require "test_helper"
+
+class Communication::Website::GitFileTest < ActiveSupport::TestCase
+  test "github should_create?" do
+    file = communication_website_git_files(:git_file_1)
+    # This is wrong! Fixtures must be prepared properly
+    assert_not file.should_create?
+  end
+
+  test "github correct sha" do
+    file = communication_website_git_files(:git_file_1)
+    assert_equal file.sha, '5d387e7e2da68026aeb0cfc2e6a67f509a3e1ff6'
+  end
+end
-- 
GitLab