DEV: Improve test suite.

DEV: Improve test suite.

diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 7ae12e8..979bf53 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -3,29 +3,14 @@
 require 'rails_helper'
 
 describe Post do
-
-  let(:user) { Fabricate(:user) }
-
-  let(:topic) { Fabricate(:private_message_topic, topic_allowed_users: [ Fabricate.build(:topic_allowed_user, user: user) ]) }
-  let(:post) { Fabricate(:post, topic: topic, user: user) }
-
-  let(:topic2) { Fabricate(:private_message_topic, topic_allowed_users: [ Fabricate.build(:topic_allowed_user, user: user) ]) }
-  let(:post2) { Fabricate(:post, topic: topic2, user: user) }
-
-  before do
-    topic.custom_fields['encrypted_title'] = '-- the encrypted title --'
-    topic.save!
-
-    SiteSetting.editing_grace_period_max_diff = 1
-
-    revisor = PostRevisor.new(post)
-    revisor.revise!(user, raw: 'base64')
-  end
+  let(:post) { Fabricate(:post) }
+  let(:encrypt_user) { Fabricate(:encrypt_user) }
+  let(:encrypt_post) { Fabricate(:encrypt_post, user: encrypt_user) }
 
   context '#is_encrypted?' do
     it 'works' do
-      expect(post.is_encrypted?).to eq(true)
+      expect(post.is_encrypted?).to eq(false)
+      expect(encrypt_post.is_encrypted?).to eq(true)
     end
   end
-
 end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
new file mode 100644
index 0000000..5a888bb
--- /dev/null
+++ b/spec/models/topic_spec.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Topic do
+  let(:topic) { Fabricate(:topic) }
+  let(:encrypt_topic) { Fabricate(:encrypt_topic) }
+
+  context '#is_encrypted?' do
+    it 'works' do
+      expect(topic.is_encrypted?).to eq(false)
+      expect(encrypt_topic.is_encrypted?).to eq(true)
+    end
+  end
+
+  context 'remove_allowed_user' do
+    it 'deletes topic key for user' do
+      expect { encrypt_topic.remove_allowed_user(Discourse.system_user, encrypt_topic.user) }
+        .to change { TopicAllowedUser.count }.by(1)
+        .and change { PluginStoreRow.count }.by(1)
+      expect(DiscourseEncrypt::Store.get("key_#{encrypt_topic.id}_#{encrypt_topic.user_id}")).to eq(nil)
+    end
+  end
+end
diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb
new file mode 100644
index 0000000..9a69ad2
--- /dev/null
+++ b/spec/plugin_helper.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+Fabricator(:encrypt_user, from: :user) do
+  custom_fields do |attrs|
+    {
+      encrypt_public: Fabricate.sequence(:encrypt) { |i| "0$publicKey#{i}" },
+      encrypt_private: Fabricate.sequence(:encrypt) { |i| "0$privateKey#{i}" }
+    }
+  end
+end
+
+Fabricator(:encrypt_topic, from: :private_message_topic) do
+  title "A secret message"
+  topic_allowed_users do |attrs|
+    [
+      Fabricate.build(:topic_allowed_user, user: attrs[:user]),
+      Fabricate.build(:topic_allowed_user, user: Fabricate.build(:encrypt_user))
+    ]
+  end
+  custom_fields do |attrs|
+    {
+      encrypted_title: Fabricate.sequence(:encrypt) { |i| "0$encryptedTitle#{i}" }
+    }
+  end
+
+  after_create do |topic|
+    topic.topic_allowed_users.each do |allowed_user|
+      PluginStore.set(
+        "discourse-encrypt",
+        "key_#{topic.id}_#{allowed_user.user_id}",
+        Fabricate.sequence(:encrypt) { |i| "0$topicKey#{i}" }
+      )
+    end
+  end
+end
+
+Fabricator(:encrypt_post, from: :private_message_post) do
+  user
+  topic do |attrs|
+    Fabricate(:encrypt_topic,
+      user: attrs[:user],
+      created_at: attrs[:created_at],
+      topic_allowed_users: [
+        Fabricate.build(:topic_allowed_user, user: attrs[:user]),
+        Fabricate.build(:topic_allowed_user, user: Fabricate.build(:encrypt_user))
+      ]
+    )
+  end
+  raw Fabricate.sequence(:encrypt) { |i| "0$base64encryptedRaw#{i}" }
+end
diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb
index df7b532..e59e1df 100644
--- a/spec/plugin_spec.rb
+++ b/spec/plugin_spec.rb
@@ -3,7 +3,6 @@
 require 'rails_helper'
 
 describe ::DiscourseEncrypt do
-
   it 'registers current user fields' do
     expect(DiscoursePluginRegistry.serialized_current_user_fields).to include('encrypt_public', 'encrypt_private')
   end
@@ -12,5 +11,4 @@ describe ::DiscourseEncrypt do
     expect(CategoryList.preloaded_topic_custom_fields).to include('encrypted_title')
     expect(TopicList.preloaded_custom_fields).to include('encrypted_title')
   end
-
 end
diff --git a/spec/requests/encrypt_controller_spec.rb b/spec/requests/encrypt_controller_spec.rb
index 9c25af9..6848d67 100644
--- a/spec/requests/encrypt_controller_spec.rb
+++ b/spec/requests/encrypt_controller_spec.rb
@@ -3,105 +3,59 @@
 require 'rails_helper'
 
 describe ::DiscourseEncrypt::EncryptController do
-
-  let(:store) { PluginStore.new('discourse-encrypt') }
-
-  let(:user)  { Fabricate(:user) }
-  let(:user2) { Fabricate(:user) }
+  let(:user)  { Fabricate(:encrypt_user) }
+  let(:user2) { Fabricate(:encrypt_user) }
   let(:user3) { Fabricate(:user) }
 
-  let(:topic) {
-    topic = Fabricate(:private_message_topic)
-    topic.custom_fields['encrypted_title'] = '-- the encrypted title --'
-    topic.save_custom_fields
-
-    Fabricate(:topic_allowed_user, topic: topic, user: user)
-    Fabricate(:topic_allowed_user, topic: topic, user: user2)
-
-    topic
-  }
-
-  let(:other_topic) { Fabricate(:topic) }
-
-  before do
-    store.set("key_#{topic.id}_#{user.id}", '-- the key of user --')
-    store.set("key_#{topic.id}_#{user2.id}", '-- the key of user2 --')
-  end
-
   context '#update_keys' do
     it 'does not work when not logged in' do
-      put '/encrypt/keys', params: {
-        public: '-- the public key --',
-        private: '-- the private key --'
-      }
-
+      put '/encrypt/keys', params: { public: '0$publicKey', private: '0$privateKey' }
       expect(response.status).to eq(403)
     end
 
     it 'does not work when user is not allowed' do
       group = Fabricate(:group)
       SiteSetting.encrypt_groups = group.name
+      sign_in(user3)
 
-      user = Fabricate(:user)
-      sign_in(user)
-
-      put '/encrypt/keys', params: {
-        public: '-- the public key --',
-        private: '-- the private key --'
-      }
+      put '/encrypt/keys', params: { public: '0$publicKey', private: '0$privateKey' }
       expect(response.status).to eq(403)
 
-      Fabricate(:group_user, group: group, user: user)
+      group.add(user3)
 
-      put '/encrypt/keys', params: {
-        public: '-- the public key --',
-        private: '-- the private key --'
-      }
+      put '/encrypt/keys', params: { public: '0$publicKey', private: '0$privateKey' }
       expect(response.status).to eq(200)
     end
 
     it 'saves user keys' do
       sign_in(user3)
 
-      put '/encrypt/keys', params: {
-        public: '-- the public key --',
-        private: '-- the private key --'
-      }
-
+      put '/encrypt/keys', params: { public: '0$publicKey', private: '0$privateKey' }
       expect(response.status).to eq(200)
-      expect(user3.custom_fields['encrypt_public']).to eq('-- the public key --')
-      expect(user3.custom_fields['encrypt_private']).to eq('-- the private key --')
+      expect(user3.custom_fields['encrypt_public']).to eq('0$publicKey')
+      expect(user3.custom_fields['encrypt_private']).to eq('0$privateKey')
     end
 
     it 'updates user keys' do
+      old_public_key = user.custom_fields['encrypt_public']
       sign_in(user)
 
-      put '/encrypt/keys', params: {
-        public: '-- the public key --',
-        private: '-- the new private key --'
-      }
-
-      user.reload
-
+      put '/encrypt/keys', params: { public: old_public_key, private: '0$newPrivateKey' }
       expect(response.status).to eq(200)
-      expect(user.custom_fields['encrypt_public']).to eq('-- the public key --')
-      expect(user.custom_fields['encrypt_private']).to eq('-- the new private key --')
+      user.reload
+      expect(user.custom_fields['encrypt_public']).to eq(old_public_key)

[... diff too long, it was truncated ...]

GitHub sha: eeca17ac

These changes are good, but have you considered/tried using Fab helpers for performance?

3 Likes

The tests are few and small, so fab! helpers make no visible difference.

I think it’s worth doing in the future just to build a habit, because you never know when tests will be built on and people should have swapped in a fab!

3 Likes