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

pod_spec.rb « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ac5520c7341a3cf017bd86dfd36a84e6a8262ead (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
# frozen_string_literal: true

describe Pod, type: :model do
  describe ".find_or_create_by" do
    it "takes a url, and makes one by host" do
      pod = Pod.find_or_create_by(url: "https://example.org/u/maxwell")
      expect(pod.host).to eq("example.org")
    end

    it "saves the port" do
      pod = Pod.find_or_create_by(url: "https://example.org:3000/")
      expect(pod.host).to eq("example.org")
      expect(pod.port).to eq(3000)
    end

    it "ignores default ports" do
      pod = Pod.find_or_create_by(url: "https://example.org:443/")
      expect(pod.host).to eq("example.org")
      expect(pod.port).to be_nil
    end

    it "sets ssl boolean" do
      pod = Pod.find_or_create_by(url: "https://example.org/")
      expect(pod.ssl).to be true
    end

    it "updates ssl boolean if upgraded to https" do
      pod = Pod.find_or_create_by(url: "http://example.org/")
      expect(pod.ssl).to be false
      pod = Pod.find_or_create_by(url: "https://example.org/")
      expect(pod.ssl).to be true
    end

    it "does not update ssl boolean if downgraded to http" do
      pod = Pod.find_or_create_by(url: "https://example.org/")
      expect(pod.ssl).to be true
      pod = Pod.find_or_create_by(url: "http://example.org/")
      expect(pod.ssl).to be true
    end

    context "validation" do
      it "is valid" do
        pod = Pod.find_or_create_by(url: "https://example.org/")
        expect(pod).to be_valid
      end

      it "doesn't allow own pod" do
        pod = Pod.find_or_create_by(url: AppConfig.url_to("/"))
        expect(pod).not_to be_valid
      end

      it "doesn't allow own pod with default port" do
        uri = URI.parse("https://example.org/")
        allow(AppConfig).to receive(:pod_uri).and_return(uri)

        pod = Pod.find_or_create_by(url: AppConfig.url_to("/"))
        expect(pod).not_to be_valid
      end

      it "doesn't allow own pod with other scheme" do
        uri = URI.parse("https://example.org/")
        allow(AppConfig).to receive(:pod_uri).and_return(uri)

        pod = Pod.find_or_create_by(url: "http://example.org/")
        expect(pod).not_to be_valid
      end
    end
  end

  describe ".check_all!" do
    before do
      @pods = (0..4).map do
        double("pod").tap do |pod|
          expect(pod).to receive(:test_connection!)
        end
      end
      allow(Pod).to receive(:find_in_batches).and_yield(@pods)
    end

    it "calls #test_connection! on every pod" do
      Pod.check_all!
    end
  end

  describe ".check_scheduled!" do
    it "calls #test_connection! on all scheduled pods" do
      (0..4).map { FactoryBot.create(:pod) }
      FactoryBot.create(:pod, scheduled_check: true)

      expect_any_instance_of(Pod).to receive(:test_connection!)
      Pod.check_scheduled!
    end
  end

  describe "#active?" do
    it "returns true for an unchecked pod" do
      pod = FactoryBot.create(:pod)
      expect(pod.active?).to be_truthy
    end

    it "returns true for an online pod" do
      pod = FactoryBot.create(:pod, status: :no_errors)
      expect(pod.reload.active?).to be_truthy
    end

    it "returns true for a pod that is offline for less than 14 days" do
      pod = FactoryBot.create(:pod, status: :net_failed, offline_since: DateTime.now.utc - 13.days)
      expect(pod.active?).to be_truthy
    end

    it "returns false for a pod that is offline for less than 14 days" do
      pod = FactoryBot.create(:pod, status: :net_failed, offline_since: DateTime.now.utc - 15.days)
      expect(pod.active?).to be_falsey
    end
  end

  describe "#schedule_check_if_needed" do
    it "schedules the pod for the next check if it is offline" do
      pod = FactoryBot.create(:pod, status: :net_failed)
      pod.schedule_check_if_needed
      expect(pod.scheduled_check).to be_truthy
    end

    it "does nothing if the pod unchecked" do
      pod = FactoryBot.create(:pod)
      pod.schedule_check_if_needed
      expect(pod.scheduled_check).to be_falsey
    end

    it "does nothing if the pod is online" do
      pod = FactoryBot.create(:pod, status: :no_errors)
      pod.schedule_check_if_needed
      expect(pod.scheduled_check).to be_falsey
    end

    it "does nothing if the pod is scheduled for the next check" do
      pod = FactoryBot.create(:pod, status: :no_errors, scheduled_check: true)
      expect(pod).not_to receive(:update_column)
      pod.schedule_check_if_needed
    end
  end

  describe "#test_connection!" do
    before do
      @pod = FactoryBot.create(:pod)
      @now = Time.zone.now

      @result = ConnectionTester::Result.new
      @result.rt = 123
      @result.software_version = "diaspora a.b.c.d"
      @result.error = ConnectionTester::NetFailure.new("hello error!")

      expect(ConnectionTester).to receive(:check).at_least(:once).and_return(@result)
    end

    it "updates the connectivity values" do
      @result.error = nil
      @pod.test_connection!

      expect(@pod.status).to eq("no_errors")
      expect(@pod.offline?).to be_falsey
      expect(@pod.response_time).to eq(123)
      expect(@pod.checked_at).to be_within(1.second).of @now
    end

    it "resets the scheduled_check flag" do
      @pod.update(scheduled_check: true)

      @pod.test_connection!

      expect(@pod.scheduled_check).to be_falsey
    end

    it "handles a failed check" do
      @pod.test_connection!

      expect(@pod.offline?).to be_truthy
      expect(@pod.offline_since).to be_within(1.second).of @now
    end

    it "removes the error message when there was no error" do
      @pod.update(error: "old error message")

      @result.error = nil
      @pod.test_connection!

      expect(@pod.error).to be_nil
    end

    it "preserves the original offline timestamp" do
      @pod.test_connection!

      expect(@pod.offline_since).to be_within(1.second).of @now

      Timecop.travel(Time.zone.today + 30.days) do
        @pod.test_connection!
        expect(@pod.offline_since).to be_within(1.second).of @now
        expect(Time.zone.now).to be_within(1.day).of(@now + 30.days)
      end
    end
  end

  describe "#url_to" do
    it "appends the path to the pod-url" do
      pod = FactoryBot.create(:pod)
      expect(pod.url_to("/receive/public")).to eq("https://#{pod.host}/receive/public")
    end
  end

  describe "#update_offline_since" do
    let(:pod) { FactoryBot.create(:pod) }

    it "handles a successful status" do
      pod.status = :no_errors
      pod.update_offline_since

      expect(pod.offline?).to be_falsey
      expect(pod.offline_since).to be_nil
    end

    it "handles a failed status" do
      now = Time.zone.now

      pod.status = :unknown_error
      pod.update_offline_since

      expect(pod.offline?).to be_truthy
      expect(pod.offline_since).to be_within(1.second).of now
    end

    it "preserves the original offline timestamp" do
      now = Time.zone.now

      pod.status = :unknown_error
      pod.update_offline_since
      pod.save

      expect(pod.offline_since).to be_within(1.second).of now

      Timecop.travel(Time.zone.today + 30.days) do
        pod.update_offline_since
        expect(pod.offline_since).to be_within(1.second).of now
        expect(Time.zone.now).to be_within(1.day).of(now + 30.days)
      end
    end
  end
end