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

aspect_memberships_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 903e732efb233f6bba834c599580dfdb1bb9dd6a (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
# 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 AspectMembershipsController, type: :controller do
  before do
    @aspect0  = alice.aspects.first
    @aspect1  = alice.aspects.create(name: "another aspect")
    @aspect2  = bob.aspects.first

    @contact = alice.contact_for(bob.person)
    alice.getting_started = false
    alice.save
    sign_in alice, scope: :user
    allow(@controller).to receive(:current_user).and_return(alice)
    request.env["HTTP_REFERER"] = "http://" + request.host
  end

  describe "#create" do
    before do
      @person = eve.person
    end

    it "succeeds" do
      post :create, params: {person_id: bob.person.id, aspect_id: @aspect1.id}, format: :json
      expect(response).to be_success
    end

    it "creates an aspect membership" do
      expect {
        post :create, params: {person_id: bob.person.id, aspect_id: @aspect1.id}, format: :json
      }.to change {
        alice.contact_for(bob.person).aspect_memberships.count
      }.by(1)
    end

    it "creates a contact" do
      # argggg why?
      alice.contacts.reload
      expect {
        post :create, params: {person_id: @person.id, aspect_id: @aspect0.id}, format: :json
      }.to change {
        alice.contacts.size
      }.by(1)
    end

    it "does not 500 on a duplicate key error" do
      params = {person_id: @person.id, aspect_id: @aspect0.id}
      post :create, params: params, format: :json
      post :create, params: params, format: :json
      expect(response.status).to eq(400)
      expect(response.body).to eq(I18n.t("aspect_memberships.destroy.invalid_statement"))
    end

    context "json" do
      it "returns the aspect membership" do
        post :create, params: {person_id: @person.id, aspect_id: @aspect0.id}, format: :json

        contact = @controller.current_user.contact_for(@person)
        expect(response.body).to eq(AspectMembershipPresenter.new(contact.aspect_memberships.first).base_hash.to_json)
      end

      it "responds with an error message when the request failed" do
        expect(alice).to receive(:share_with).and_return(nil)
        post :create, params: {person_id: @person.id, aspect_id: @aspect0.id}, format: :json
        expect(response.status).to eq(409)
        expect(response.body).to eq(I18n.t("aspects.add_to_aspect.failure"))
      end
    end
  end

  describe "#destroy" do
    it "removes contacts from an aspect" do
      membership = alice.add_contact_to_aspect(@contact, @aspect1)
      delete :destroy, params: {id: membership.id}, format: :json
      expect(response).to be_success
      @aspect1.reload
      expect(@aspect1.contacts.to_a).not_to include @contact
    end

    it "aspect membership does not exist" do
      delete :destroy, params: {id: 123}, format: :json
      expect(response).not_to be_success
      expect(response.body).to eq(I18n.t("aspect_memberships.destroy.no_membership"))
    end
  end
end