FIX: Create topic keys for all users on new PM (#83)

FIX: Create topic keys for all users on new PM (#83)

Creating encrypted PMs on the server side generated topic keys only for the other participants, ignoring the user who created the topic.

diff --git a/lib/encrypted_post_creator.rb b/lib/encrypted_post_creator.rb
index d216dbf..e527414 100644
--- a/lib/encrypted_post_creator.rb
+++ b/lib/encrypted_post_creator.rb
@@ -54,11 +54,7 @@ class EncryptedPostCreator < PostCreator
   private
 
   def users
-    @users ||= begin
-      usernames = @opts[:target_usernames].split(',').map(&:downcase)
-      users = User.where(username_lower: usernames)
-      users
-    end
+    @users ||= User.where(username_lower: @opts[:target_usernames].split(',').map(&:downcase)).to_a << @user
   end
 
   def self.encrypt(raw, key)
diff --git a/spec/lib/encrypted_post_creator_spec.rb b/spec/lib/encrypted_post_creator_spec.rb
index 9f15f5e..469ae1e 100644
--- a/spec/lib/encrypted_post_creator_spec.rb
+++ b/spec/lib/encrypted_post_creator_spec.rb
@@ -3,32 +3,40 @@
 require 'rails_helper'
 
 describe EncryptedPostCreator do
-  let(:user) { Fabricate(:user) }
+  let(:public_key) do
+    <<~EOS
+    1$eyJlbmNyeXB0UHVibGljIjp7ImFsZyI6IlJTQS1PQUVQLTI1NiIsImUiOiJBUUFCIiwiZXh0Ijp0cnVlLCJrZXlfb3BzIjpbImVuY3J5cHQiLCJ3cmFwS2V5Il0sImt0eSI6IlJTQSIsIm4iOiIzY3Nn
+    NWN4ZFJ4SUF6c041dk5EckwteExvNjRCSkNLM25KeGF5NHV1U280SEpYSzVyT2VZbjBueTdkUmc1LUlKT1ZxS0tNTXF4c2JVT0xyang3WkFDZU50Y2Qyc3NSaXo1UWNiaTU4Nl9jc0hrRldZOFpMd0Y2Vk
+    JJWGxONlZpdHVqWkU4cDlVVjZIR2J6RjBqYjZfRWhkSlNQb3lWR3R5N3Naem9mSTktdGc0ZDFKcHhkWmxMODJ6WVpONEl1a2N6Tkc3NjVfcjNYb1RyYlVEOFBqc0Mxa1hPZnRPTjBtU2tNSUZjQlFHY0M5
+    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VDl1aG5GSnVFdUxySWZPenJ4M3pEMXMwYTRtTV
+    lIQkZobENuTFBneS1PT1MxQl8tY0RFeUpFbjQ1M2Jra3phd0NmOEJQNEJ5ODhjMVhyeXEwdmduamNqRTFQZ2dITDNDR2U0QVRNVEVPSmloSlkyTGN2VjdNNzRLeXR1WXV3OS1xMjA4VWpCMDM0YnlRX182
+    OXoyQnhXUlIwemdmdWNuODFjSEdVd0RCa0cxVWZuWF9qTkFyX3BNdE9OaWpsZ25nbjdrX2dibi14MVFLWFlPVjhQTUp6RXNSMTA4SmR6V1FsalEzVV9rZVZEYy1FX0VKRGp6ZHhST1Y1SHE1Wi1FYktzOW
+    9rZ0RjWUxnc2pQLTZKNjRUMklMX01QRW9hdnltMmtqQVFNZXlWZzZOc1dJN0RRTmNfU0RFLUw2bjdHWnBKdER6ZkI2b1hNOHE1TlRKMV8zcXZCUENCRzljMjJTZWs3c0s5c09zeEFxRGZ3aU5vU2NLR3Jf
+    aHRWSTZKQlYwU2xueVJPSDJsWVFwX0FEc3paakN4X2tDdjNGTmlMR1FlUkRDUmtpWHlqX0c0Skhnb0tSeDFDLWV5MCJ9fQ==
+    EOS
+  end
+
+  let(:user) do
+    Fabricate(:user).tap do |user|
+      UserEncryptionKey.create!(user: user, encrypt_public: public_key)
+    end
+  end
+
   let(:topic) { Fabricate(:topic) }
 
   let(:destination_user) do
     Fabricate(:user).tap do |user|
-      public_key = <<EOS
-1$eyJlbmNyeXB0UHVibGljIjp7ImFsZyI6IlJTQS1PQUVQLTI1NiIsImUiOiJBUUFCIiwiZXh0Ijp0cnVlLCJrZXlfb3BzIjpbImVuY3J5cHQiLCJ3cmFwS2V5Il0sImt0eSI6IlJTQSIsIm4iOiIzY3Nn
-NWN4ZFJ4SUF6c041dk5EckwteExvNjRCSkNLM25KeGF5NHV1U280SEpYSzVyT2VZbjBueTdkUmc1LUlKT1ZxS0tNTXF4c2JVT0xyang3WkFDZU50Y2Qyc3NSaXo1UWNiaTU4Nl9jc0hrR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RDhPcEl5VEVid09jSEtwMVBKZkdQTWs5NDB1Wm50aVRCQzA2T2Fm
-MWRpcFB4eGpXT2lGRFhGcUF0SkozQTRNWldoUGU3c3VVOEJjMG8wMkR5Wmsxa0pfU1p0RnJUSW5GVlpITnlsQ1A0R0d5azlUakdoTkRoSVV1QTFZVUstVDl1aG5GSnVFdUxySWZPenJ4M3pEMXMwYTRtTV
-lIQkZobENuTFBneS1PT1MxQl8tY0RFeUpFbjQ1M2Jra3phd0NmOEJQNEJ5ODhjMVhyeXEwdmduamNqRTFQZ2dITDNDR2U0QVRNVEVPSmloSlkyTGN2VjdNNzRLeXR1WXV3OS1xMjA4VWpCMDM0YnlRX182
-OXoyQnhXUlIwemdmdWNuODFjSEdVd0RCa0cxVWZuWF9qTkFyX3BNdE9OaWpsZ25nbjdrX2dibi14MVFLWFlPVjhQTUp6RXNSMTA4SmR6V1FsalEzVV9rZVZEYy1FX0VKRGp6ZHhST1Y1SHE1Wi1FYktzOW
-9rZ0RjWUxnc2pQLTZKNjRUMklMX01QRW9hdnltMmtqQVFNZXlWZzZOc1dJN0RRTmNfU0RFLUw2bjdHWnBKdER6ZkI2b1hNOHE1TlRKMV8zcXZCUENCRzljMjJTZWs3c0s5c09zeEFxRGZ3aU5vU2NLR3Jf
-aHRWSTZKQlYwU2xueVJPSDJsWVFwX0FEc3paakN4X2tDdjNGTmlMR1FlUkRDUmtpWHlqX0c0Skhnb0tSeDFDLWV5MCJ9fQ==
-EOS
       UserEncryptionKey.create!(user: user, encrypt_public: public_key)
     end
   end
 
-  context '#create_encrypted' do
+  context '#create' do
     it 'ensures archetype is private message' do
       creator = described_class.new(user, raw: 'Hello world!')
       post = creator.create
@@ -66,5 +74,19 @@ EOS
       expect(post.is_encrypted?).to eq(true)
       expect(post.raw).not_to eq('Hello world!')
     end
+
+    it 'creates topic keys for all allowed users' do
+      creator = described_class.new(user,
+        title: 'Hello world!',
+        raw: 'Hello world!',
+        target_usernames: destination_user.username,
+        archetype: Archetype.private_message
+      )
+
+      post = creator.create
+      expect(post.topic.topic_allowed_users.map(&:user)).to contain_exactly(user, destination_user)
+      expect(EncryptedTopicsUser.find_by(user: user, topic: post.topic)).to be_present
+      expect(EncryptedTopicsUser.find_by(user: destination_user, topic: post.topic)).to be_present
+    end
   end
 end

GitHub sha: 1492adbc

This commit appears in #83 which was approved by ZogStriP. It was merged by ZogStriP.