DEV: Add specs for Ruby code.

DEV: Add specs for Ruby code.

From a122de837d02c23361024d3d0296af416a1f93e2 Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Tue, 4 Dec 2018 19:54:37 +0200
Subject: [PATCH] DEV: Add specs for Ruby code.


diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
new file mode 100644
index 0000000..a1fa4f2
--- /dev/null
+++ b/spec/models/post_spec.rb
@@ -0,0 +1,36 @@
+require 'rails_helper'
+
+describe Post do
+
+  let(:user) { Fabricate(:user) }
+
+  let(:topic) { Fabricate(:topic) }
+  let(:post) { Fabricate(:post, topic: topic, user: user) }
+
+  let(:topic2) { Fabricate(:topic) }
+  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: 'this post is encrypted and has been edited')
+
+    revisor = PostRevisor.new(post2)
+    revisor.revise!(user, raw: 'this post is unencrypted and has been edited')
+  end
+
+  it 'hides version for encrypted posts' do
+    expect(post.version).to eq(1)
+    expect(post.public_version).to eq(1)
+  end
+
+  it 'shows real version for unencrypted posts' do
+    expect(post2.version).to eq(2)
+    expect(post2.public_version).to eq(2)
+  end
+
+end
diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb
new file mode 100644
index 0000000..8ff518e
--- /dev/null
+++ b/spec/plugin_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+describe ::DiscourseEncrypt do
+
+  it 'registers current user fields' do
+    expect(DiscoursePluginRegistry.serialized_current_user_fields).to include('encrypt_public_key', 'encrypt_private_key', 'encrypt_salt')
+  end
+
+  it 'registers preloaded custom fields' 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
new file mode 100644
index 0000000..2449663
--- /dev/null
+++ b/spec/requests/encrypt_controller_spec.rb
@@ -0,0 +1,202 @@
+require 'rails_helper'
+
+describe ::DiscourseEncrypt::EncryptController do
+
+  let(:store) { PluginStore.new('discourse-encrypt') }
+
+  let(:user) { Fabricate(:user, username: 'user') }
+  let(:user2) { Fabricate(:user, username: 'user2') }
+  let(:other_user) { Fabricate(:user, username: 'other') }
+
+  let(:topic) { Fabricate(:topic) }
+  let(:other_topic) { Fabricate(:topic) }
+
+  before do
+    user.custom_fields['encrypt_public_key'] = '-- the public key --'
+    user.custom_fields['encrypt_private_key'] = '-- the private key --'
+    user.custom_fields['encrypt_salt'] = '-- the salt --'
+    user.save!
+
+    user2.custom_fields['encrypt_public_key'] = '-- another public key --'
+    user2.custom_fields['encrypt_private_key'] = '-- another private key --'
+    user2.custom_fields['encrypt_salt'] = '-- another salt --'
+    user2.save!
+
+    topic.custom_fields['encrypted_title'] = '-- the encrypted title --'
+    topic.save!
+
+    store.set("key_#{topic.id}_#{user.id}", '-- the key of user --')
+    store.set("key_#{topic.id}_#{user2.id}", '-- the key of user2 --')
+  end
+
+  context '#keys_put' do
+    it 'does not work when not logged in' do
+      put '/encrypt/keys', params: {
+        public_key: '-- the public key --',
+        private_key: '-- the private key --',
+        salt: '-- the salt --'
+      }
+
+      expect(response.status).to eq(403)
+    end
+
+    it 'saves user keys' do
+      sign_in(other_user)
+
+      put '/encrypt/keys', params: {
+        public_key: '-- the public key --',
+        private_key: '-- the private key --',
+        salt: '-- the salt --'
+      }
+
+      expect(response.status).to eq(200)
+      expect(other_user.custom_fields['encrypt_public_key']).to eq('-- the public key --')
+      expect(other_user.custom_fields['encrypt_private_key']).to eq('-- the private key --')
+      expect(other_user.custom_fields['encrypt_salt']).to eq('-- the salt --')
+    end
+
+    it 'updates user keys' do
+      sign_in(user)
+
+      put '/encrypt/keys', params: {
+        public_key: '-- the public key --',
+        private_key: '-- the new private key --',
+        salt: '-- the new salt --'
+      }
+
+      user.reload
+
+      expect(response.status).to eq(200)
+      expect(user.custom_fields['encrypt_public_key']).to eq('-- the public key --')
+      expect(user.custom_fields['encrypt_private_key']).to eq('-- the new private key --')
+      expect(user.custom_fields['encrypt_salt']).to eq('-- the new salt --')
+    end
+
+    it 'does not allow updating if wrong public key' do
+      sign_in(user)
+
+      put '/encrypt/keys', params: {
+        public_key: '-- a wrong public key --',
+        private_key: '-- the new private key --',
+        salt: '-- the new salt --'
+      }
+
+      expect(response.status).to eq(409)
+      expect(user.custom_fields['encrypt_public_key']).to eq('-- the public key --')
+      expect(user.custom_fields['encrypt_private_key']).to eq('-- the private key --')
+      expect(user.custom_fields['encrypt_salt']).to eq('-- the salt --')
+    end
+  end
+
+  context '#user_get' do
+    it 'does not work when not logged in' do
+      get '/encrypt/user', params: { usernames: [ 'user', 'user2', 'other' ] }
+      expect(response.status).to eq(404)
+    end
+
+    it 'gets the right user keys' do
+      sign_in(user)
+
+      get '/encrypt/user', params: { usernames: [ 'user', 'user2', 'other' ] }
+
+      expect(response.status).to eq(200)
+      json = ::JSON.parse(response.body)
+      expect(json.size).to eq(2)
+      expect(json['user']).to eq('-- the public key --')
+      expect(json['user2']).to eq('-- another public key --')
+      expect(json['other']).to eq(nil)
+    end
+  end
+
+  context '#topic_put' do
+    it 'does not work when logged in' do
+      put '/encrypt/topic', params: {
+        topic_id: topic.id,
+        title: '-- other encrypted title --',
+        keys: {
+          user: '-- other key of user --',
+          user2: '-- other key of user2 --'
+        }
+      }
+
+      expect(topic.custom_fields['encrypted_title']).to eq('-- the encrypted title --')
+      expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
+      expect(store.get("key_#{topic.id}_#{user2.id}")).to eq('-- the key of user2 --')
+    end
+
+    it 'saves encrypted topic data' do
+      sign_in(user)
+
+      put '/encrypt/topic', params: {
+        topic_id: other_topic.id,
+        title: '-- other encrypted title --',
+        keys: {
+          user: '-- other key of user --',
+          user2: '-- other key of user2 --'
+        }
+      }
+
+      expect(response.status).to eq(200)
+      expect(other_topic.custom_fields['encrypted_title']).to eq('-- other encrypted title --')
+      expect(store.get("key_#{other_topic.id}_#{user.id}")).to eq('-- other key of user --')
+      expect(store.get("key_#{other_topic.id}_#{user2.id}")).to eq('-- other key of user2 --')
+    end
+
+    it 'saves encrypted topic title' do
+      sign_in(user)
+
+      put '/encrypt/topic', params: {
+        topic_id: topic.id,
+        title: '-- new encrypted title --',
+      }
+
+      topic.reload
+
+      expect(response.status).to eq(200)
+      expect(topic.custom_fields['encrypted_title']).to eq('-- new encrypted title --')
+      expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
+      expect(store.get("key_#{topic.id}_#{user2.id}")).to eq('-- the key of user2 --')
+    end
+
+    it 'saves encrypted topic key for each user' do
+      sign_in(user)
+
+      put '/encrypt/topic', params: {
+        topic_id: topic.id,
+        keys: {
+          user: '-- new key of user --',
+          user2: '-- new key of user2 --'
+        }
+      }
+
+      expect(response.status).to eq(200)
+      expec

GitHub

1 Like

I would just do the 4 assertions in one clause otherwise you are doing all this setup again for no real reason

do you need all of this setup every time? instead by putting bits in let you will only spin up the entities you need for the test

same thing as earlier, setup here is very expensive so it may make sense to just collapse the tests

1 Like

@udan11 I think you already sorted all of these, in the other commits, no?

1 Like

Yes, that is correct.

1 Like