Welcome to mirror list, hosted at ThFree Co, Russian Federation.

exporter_spec.rb « integration « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7794c07de43ce00a27563413845a1371717c323b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

describe Diaspora::Exporter do
  let(:user) { FactoryGirl.create(:user_with_aspect) }

  context "output json" do
    let(:json) { Diaspora::Exporter.new(user).execute }

    it "matches archive schema" do
      DataGenerator.create(
        user,
        %i[generic_user_data activity status_messages_flavours work_aspect]
      )

      expect(JSON.parse(json)).to match_json_schema(:archive_schema)
    end

    it "contains basic user data" do
      user_properties = build_property_hash(
        user,
        %i[email username language disable_mail show_community_spotlight_in_stream auto_follow_back
           auto_follow_back_aspect strip_exif],
        private_key: :serialized_private_key
      )

      user_properties[:profile] = {
        entity_type: "profile",
        entity_data: build_property_hash(
          user.profile,
          %i[first_name last_name gender bio location image_url birthday searchable nsfw tag_string],
          author: :diaspora_handle
        )
      }

      expect(json).to include_json(user: user_properties)
    end

    it "contains aspects" do
      DataGenerator.create(user, :work_aspect)

      expect(json).to include_json(
        user: {
          "contact_groups": [
            {
              "name": "generic"
            },
            {
              "name": "Work"
            }
          ]
        }
      )
    end

    it "contains contacts" do
      friends = DataGenerator.create(user, Array.new(2, :mutual_friend))
      serialized_contacts = friends.map {|friend|
        contact = Contact.find_by(person_id: friend.person_id)
        hash = build_property_hash(
          contact,
          %i[sharing receiving person_guid person_name],
          following: :sharing, followed: :receiving, account_id: :person_diaspora_handle
        )
        hash[:public_key] = contact.person.serialized_public_key
        hash[:contact_groups_membership] = contact.aspects.map(&:name)
        hash
      }

      expect(json).to include_json(user: {contacts: serialized_contacts})
    end

    it "contains a public status message" do
      status_message = FactoryGirl.create(:status_message, author: user.person, public: true)
      serialized = {
        "subscribed_pods_uris": [AppConfig.pod_uri.to_s],
        "entity_type":          "status_message",
        "entity_data":          {
          "author":     user.diaspora_handle,
          "guid":       status_message.guid,
          "created_at": status_message.created_at.iso8601,
          "text":       status_message.text,
          "public":     true
        }
      }

      expect(json).to include_json(user: {posts: [serialized]})
    end

    it "contains a status message with subscribers" do
      subscriber, status_message = DataGenerator.create(user, :status_message_with_subscriber)
      serialized = {
        "subscribed_users_ids": [subscriber.diaspora_handle],
        "entity_type":          "status_message",
        "entity_data":          {
          "author":     user.diaspora_handle,
          "guid":       status_message.guid,
          "created_at": status_message.created_at.iso8601,
          "text":       status_message.text,
          "public":     false
        }
      }

      expect(json).to include_json(user: {posts: [serialized]})
    end

    it "contains a status message with a poll" do
      status_message = FactoryGirl.create(:status_message_with_poll, author: user.person)
      serialized = {
        "entity_type": "status_message",
        "entity_data": {
          "author":     user.diaspora_handle,
          "guid":       status_message.guid,
          "created_at": status_message.created_at.iso8601,
          "text":       status_message.text,
          "poll":       {
            "entity_type": "poll",
            "entity_data": {
              "guid":         status_message.poll.guid,
              "question":     status_message.poll.question,
              "poll_answers": status_message.poll.poll_answers.map {|answer|
                {
                  "entity_type": "poll_answer",
                  "entity_data": {
                    "guid":   answer.guid,
                    "answer": answer.answer
                  }
                }
              }
            }
          },
          "public":     false
        }
      }

      expect(json).to include_json(user: {posts: [serialized]})
    end

    it "contains a status message with a photo" do
      status_message = FactoryGirl.create(:status_message_with_photo, author: user.person)

      serialized = {
        "entity_type": "status_message",
        "entity_data": {
          "author":     user.diaspora_handle,
          "guid":       status_message.guid,
          "created_at": status_message.created_at.iso8601,
          "text":       status_message.text,
          "photos":     [
            {
              "entity_type": "photo",
              "entity_data": {
                "guid":                status_message.photos.first.guid,
                "author":              user.diaspora_handle,
                "public":              false,
                "created_at":          status_message.photos.first.created_at.iso8601,
                "remote_photo_path":   "#{AppConfig.pod_uri}uploads\/images\/",
                "remote_photo_name":   status_message.photos.first.remote_photo_name,
                "status_message_guid": status_message.guid,
                "height":              42,
                "width":               23
              }
            }
          ],
          "public":     false
        }
      }

      expect(json).to include_json(user: {posts: [serialized]})
    end

    it "contains a status message with a location" do
      status_message = FactoryGirl.create(:status_message_with_location, author: user.person)

      serialized = {
        "entity_type": "status_message",
        "entity_data": {
          "author":     user.diaspora_handle,
          "guid":       status_message.guid,
          "created_at": status_message.created_at.iso8601,
          "text":       status_message.text,
          "location":   {
            "entity_type": "location",
            "entity_data": {
              "address": status_message.location.address,
              "lat":     status_message.location.lat,
              "lng":     status_message.location.lng
            }
          },
          "public":     false
        }
      }

      expect(json).to include_json(user: {posts: [serialized]})
    end

    it "contains a reshare" do
      reshare = FactoryGirl.create(:reshare, author: user.person)
      serialized_reshare = {
        "subscribed_pods_uris": [reshare.root.author.pod.url_to(""), AppConfig.pod_uri.to_s],
        "entity_type":          "reshare",
        "entity_data":          {
          "author":      user.diaspora_handle,
          "guid":        reshare.guid,
          "created_at":  reshare.created_at.iso8601,
          "root_author": reshare.root_author.diaspora_handle,
          "root_guid":   reshare.root_guid
        }
      }

      expect(json).to include_json(
        user: {posts: [serialized_reshare]}
      )
    end

    it "contains followed tags" do
      tag_following = DataGenerator.create(user, :tag_following)
      expect(json).to include_json(user: {followed_tags: [tag_following.tag.name]})
    end

    it "contains post subscriptions" do
      subscription = DataGenerator.create(user, :subscription)
      expect(json).to include_json(user: {post_subscriptions: [subscription.target.guid]})
    end

    it "contains a comment" do
      comment = FactoryGirl.create(:comment, author: user.person)
      serialized_comment = {
        "entity_type":    "comment",
        "entity_data":    {
          "author":      user.diaspora_handle,
          "guid":        comment.guid,
          "parent_guid": comment.parent.guid,
          "text":        comment.text,
          "created_at":  comment.created_at.iso8601
        },
        "property_order": %w[author guid parent_guid text created_at]
      }

      expect(json).to include_json(
        user: {relayables: [serialized_comment]}
      )
    end

    it "contains a like" do
      like = FactoryGirl.create(:like, author: user.person)
      serialized_like = {
        "entity_type":    "like",
        "entity_data":    {
          "author":      user.diaspora_handle,
          "guid":        like.guid,
          "parent_guid": like.parent.guid,
          "parent_type": like.target_type,
          "positive":    like.positive
        },
        "property_order": %w[author guid parent_guid parent_type positive]
      }

      expect(json).to include_json(
        user: {relayables: [serialized_like]}
      )
    end

    it "contains a poll participation" do
      poll_participation = FactoryGirl.create(:poll_participation, author: user.person)
      serialized_participation = {
        "entity_type":    "poll_participation",
        "entity_data":    {
          "author":           user.diaspora_handle,
          "guid":             poll_participation.guid,
          "parent_guid":      poll_participation.parent.guid,
          "poll_answer_guid": poll_participation.poll_answer.guid
        },
        "property_order": %w[author guid parent_guid poll_answer_guid]
      }

      expect(json).to include_json(
        user: {relayables: [serialized_participation]}
      )
    end

    it "contains a comment for the user's post" do
      status_message, comment = DataGenerator.create(user, :status_message_with_comment)
      serialized = {
        "entity_type":    "comment",
        "entity_data":    {
          "author":           comment.diaspora_handle,
          "guid":             comment.guid,
          "parent_guid":      status_message.guid,
          "text":             comment.text,
          "created_at":       comment.created_at.iso8601,
          "author_signature": Diaspora::Federation::Entities.build(comment).to_h[:author_signature]
        },
        "property_order": %w[author guid parent_guid text created_at]
      }

      expect(json).to include_json(others_data: {relayables: [serialized]})
    end

    it "contains a like for the user's post" do
      status_message, like = DataGenerator.create(user, :status_message_with_like)
      serialized = {
        "entity_type":    "like",
        "entity_data":    {
          "author":           like.diaspora_handle,
          "guid":             like.guid,
          "parent_guid":      status_message.guid,
          "parent_type":      like.target_type,
          "positive":         like.positive,
          "author_signature": Diaspora::Federation::Entities.build(like).to_h[:author_signature]
        },
        "property_order": %w[author guid parent_guid parent_type positive]
      }

      expect(json).to include_json(others_data: {relayables: [serialized]})
    end

    it "contains a poll participation for the user's post" do
      _, poll_participation = DataGenerator.create(user, :status_message_with_poll_participation)
      serialized = {
        "entity_type":    "poll_participation",
        "entity_data":    {
          "author":           poll_participation.diaspora_handle,
          "guid":             poll_participation.guid,
          "parent_guid":      poll_participation.parent.guid,
          "poll_answer_guid": poll_participation.poll_answer.guid,
          "author_signature": Diaspora::Federation::Entities.build(poll_participation).to_h[:author_signature]
        },
        "property_order": %w[author guid parent_guid poll_answer_guid]
      }

      expect(json).to include_json(others_data: {relayables: [serialized]})
    end

    def transform_value(value)
      return value.iso8601 if value.is_a? Date
      value
    end

    def build_property_hash(object, direct_properties, aliased_properties={})
      props = direct_properties.map {|key|
        [key, transform_value(object.send(key))]
      }.to_h

      aliased = aliased_properties.map {|key, key_alias|
        [key, object.send(key_alias)]
      }.to_h

      props.merge(aliased)
    end
  end
end