DEV: Optimize tests.

DEV: Optimize tests.
From b38c12e3127428d032e491a798f5fb4b2d3ca967 Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Wed, 5 Dec 2018 16:19:00 +0200
Subject: [PATCH] DEV: Optimize tests.


diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index a2d512b..3fd221f 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -26,9 +26,7 @@ describe Post do
   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
diff --git a/spec/requests/encrypt_controller_spec.rb b/spec/requests/encrypt_controller_spec.rb
index 7b4c70f..e7a0c6e 100644
--- a/spec/requests/encrypt_controller_spec.rb
+++ b/spec/requests/encrypt_controller_spec.rb
@@ -4,36 +4,25 @@ 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(:user) { Fabricate(:user) }
+  let(:user2) { Fabricate(:user) }
+  let(:user3) { Fabricate(:user) }
 
   let(:topic) {
-    Fabricate(
+    topic = Fabricate(
       :private_message_topic,
       topic_allowed_users: [
         Fabricate.build(:topic_allowed_user, user: user),
         Fabricate.build(:topic_allowed_user, user: user2)
       ]
     )
+    topic.custom_fields['encrypted_title'] = '-- the encrypted title --'
+    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
@@ -50,7 +39,7 @@ describe ::DiscourseEncrypt::EncryptController do
     end
 
     it 'saves user keys' do
-      sign_in(other_user)
+      sign_in(user3)
 
       put '/encrypt/keys', params: {
         public_key: '-- the public key --',
@@ -59,9 +48,9 @@ describe ::DiscourseEncrypt::EncryptController do
       }
 
       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 --')
+      expect(user3.custom_fields['encrypt_public_key']).to eq('-- the public key --')
+      expect(user3.custom_fields['encrypt_private_key']).to eq('-- the private key --')
+      expect(user3.custom_fields['encrypt_salt']).to eq('-- the salt --')
     end
 
     it 'updates user keys' do
@@ -82,6 +71,10 @@ describe ::DiscourseEncrypt::EncryptController do
     end
 
     it 'does not allow updating if wrong public key' 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!
       sign_in(user)
 
       put '/encrypt/keys', params: {
@@ -99,21 +92,25 @@ describe ::DiscourseEncrypt::EncryptController do
 
   context '#show_user' do
     it 'does not work when not logged in' do
-      get '/encrypt/user', params: { usernames: [ 'user', 'user2', 'other' ] }
+      get '/encrypt/user', params: { usernames: [ user.username, user2.username, user3.username ] }
       expect(response.status).to eq(404)
     end
 
     it 'gets the right user keys' do
+      user.custom_fields['encrypt_public_key'] = '-- the public key --'
+      user.save!
+      user2.custom_fields['encrypt_public_key'] = '-- another public key --'
+      user2.save
       sign_in(user)
 
-      get '/encrypt/user', params: { usernames: [ 'user', 'user2', 'other' ] }
+      get '/encrypt/user', params: { usernames: [ user.username, user2.username, user3.username ] }
 
       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)
+      expect(json.size).to eq(3)
+      expect(json[user.username]).to eq('-- the public key --')
+      expect(json[user2.username]).to eq('-- another public key --')
+      expect(json[user3.username]).to eq(nil)
     end
   end
 
@@ -123,8 +120,8 @@ describe ::DiscourseEncrypt::EncryptController do
         topic_id: topic.id,
         title: '-- other encrypted title --',
         keys: {
-          user: '-- other key of user --',
-          user2: '-- other key of user2 --'
+          user.username => '-- other key of user --',
+          user2.username => '-- other key of user2 --'
         }
       }
 
@@ -134,14 +131,14 @@ describe ::DiscourseEncrypt::EncryptController do
     end
 
     it 'does not work for users who cannot see topic' do
-      sign_in(other_user)
+      sign_in(user3)
 
       put '/encrypt/topic', params: {
         topic_id: topic.id,
         title: '-- other encrypted title --',
         keys: {
-          user: '-- other key of user --',
-          user2: '-- other key of user2 --'
+          user.username => '-- other key of user --',
+          user2.username => '-- other key of user2 --'
         }
       }
 
@@ -157,8 +154,8 @@ describe ::DiscourseEncrypt::EncryptController do
         topic_id: other_topic.id,
         title: '-- other encrypted title --',
         keys: {
-          user: '-- other key of user --',
-          user2: '-- other key of user2 --'
+          user.username => '-- other key of user --',
+          user2.username => '-- other key of user2 --'
         }
       }
 
@@ -190,8 +187,8 @@ describe ::DiscourseEncrypt::EncryptController do
       put '/encrypt/topic', params: {
         topic_id: topic.id,
         keys: {
-          user: '-- new key of user --',
-          user2: '-- new key of user2 --'
+          user.username => '-- new key of user --',
+          user2.username => '-- new key of user2 --'
         }
       }
 
@@ -204,7 +201,7 @@ describe ::DiscourseEncrypt::EncryptController do
 
   context '#destroy_topic' do
     it 'does not work when not logged in' do
-      delete '/encrypt/topic', params: { topic_id: topic.id, usernames: [ 'user' ] }
+      delete '/encrypt/topic', params: { topic_id: topic.id, usernames: [ user.username ] }
 
       expect(response.status).to eq(403)
       expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
@@ -212,9 +209,9 @@ describe ::DiscourseEncrypt::EncryptController do
     end
 
     it 'does not work for users who cannot see topic' do
-      sign_in(other_user)
+      sign_in(user3)
 
-      delete '/encrypt/topic', params: { topic_id: topic.id, usernames: [ 'user' ] }
+      delete '/encrypt/topic', params: { topic_id: topic.id, usernames: [ user.username ] }
 
       expect(response.status).to eq(403)
       expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
@@ -227,7 +224,7 @@ describe ::DiscourseEncrypt::EncryptController do
       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 --')
 
-      delete '/encrypt/topic', params: { topic_id: topic.id,

GitHub

2 Likes