forked from hackorum-dev/hackorum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopics_spec.rb
More file actions
182 lines (151 loc) · 6.4 KB
/
topics_spec.rb
File metadata and controls
182 lines (151 loc) · 6.4 KB
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
require 'rails_helper'
RSpec.describe "Topics", type: :request do
describe "GET /topics" do
context "when there are topics with messages" do
let!(:creator1) { create(:alias) }
let!(:creator2) { create(:alias) }
let!(:topic1) { create(:topic, creator: creator1, created_at: 2.days.ago) }
let!(:topic2) { create(:topic, creator: creator2, created_at: 1.day.ago) }
let!(:message1) { create(:message, topic: topic1, sender: creator1, created_at: 2.days.ago) }
let!(:message2) { create(:message, topic: topic2, sender: creator2, created_at: 1.day.ago) }
it "returns http success" do
get topics_path
expect(response).to have_http_status(:success)
end
it "renders the index page" do
get topics_path
expect(response.body).to include("PostgreSQL Hackers Archive")
end
it "displays topic titles" do
get topics_path
expect(response.body).to include(topic1.title)
expect(response.body).to include(topic2.title)
end
it "shows topics with most recent activity first" do
get topics_path
topic1_position = response.body.index(topic1.title)
topic2_position = response.body.index(topic2.title)
expect(topic2_position).to be < topic1_position
end
it "displays creator names" do
get topics_path
expect(response.body).to include(creator1.name)
expect(response.body).to include(creator2.name)
end
end
context "when there are no topics" do
it "returns http success with empty state" do
get topics_path
expect(response).to have_http_status(:success)
expect(response.body).to include("topics-table")
end
end
end
describe "GET /topics/:id" do
let!(:creator) { create(:alias) }
let!(:topic) { create(:topic, creator: creator) }
let!(:root_message) { create(:message, topic: topic, sender: creator, reply_to: nil, created_at: 2.hours.ago) }
let!(:reply_message) { create(:message, topic: topic, sender: creator, reply_to: root_message, created_at: 1.hour.ago) }
context "with default parameters" do
it "returns http success" do
get topic_path(topic)
expect(response).to have_http_status(:success)
end
it "displays the topic title" do
get topic_path(topic)
expect(response.body).to include(topic.title)
end
it "displays messages" do
get topic_path(topic)
expect(response.body).to include(root_message.body)
expect(response.body).to include(reply_message.body)
end
it "shows flat view (oldest first)" do
get topic_path(topic)
expect(response.body).to include('messages-container flat')
root_position = response.body.index(root_message.body)
reply_position = response.body.index(reply_message.body)
expect(root_position).to be < reply_position
end
it "renders patch attachments inline as expandable diff blocks" do
create(:attachment, :patch_file, message: root_message)
get topic_path(topic)
expect(response).to have_http_status(:success)
expect(response.body).to include("Attachments:")
expect(response.body).to include('class="language-diff"')
expect(response.body).to include("diff --git")
end
end
context "with nonexistent topic" do
it "returns 404" do
get topic_path(id: 99999)
expect(response).to have_http_status(:not_found)
end
end
end
describe "GET /attachments/:id" do
let!(:creator) { create(:alias) }
let!(:topic) { create(:topic, creator: creator) }
let!(:message) { create(:message, topic: topic, sender: creator, reply_to: nil, created_at: 2.hours.ago) }
it "streams the attachment as a download with the right filename" do
attachment = create(:attachment, :patch_file, message: message)
get attachment_path(attachment)
expect(response).to have_http_status(:success)
expect(response.headers["Content-Disposition"]).to include("attachment")
expect(response.headers["Content-Disposition"]).to include(attachment.file_name)
expect(response.body).to include("diff --git")
end
it "returns 404 when the attachment body is missing" do
attachment = create(:attachment, body: nil, message: message)
get attachment_path(attachment)
expect(response).to have_http_status(:not_found)
end
end
describe "GET /topics/search" do
let!(:creator1) { create(:alias) }
let!(:creator2) { create(:alias) }
let!(:topic1) { create(:topic, title: "PostgreSQL Performance Tuning", creator: creator1) }
let!(:topic2) { create(:topic, title: "MySQL vs PostgreSQL", creator: creator2) }
let!(:message1) { create(:message, topic: topic1, body: "Performance optimization tips", sender: creator1) }
let!(:message2) { create(:message, topic: topic2, body: "Database comparison", sender: creator2) }
context "with search query" do
it "returns http success" do
get search_topics_path, params: { q: "PostgreSQL" }
expect(response).to have_http_status(:success)
end
it "displays search results" do
get search_topics_path, params: { q: "Performance" }
expect(response.body).to include(topic1.title)
expect(response.body).not_to include(topic2.title)
end
it "shows the search query" do
get search_topics_path, params: { q: "PostgreSQL" }
expect(response.body).to include('value="PostgreSQL"')
end
it "finds topics by message content" do
get search_topics_path, params: { q: "optimization" }
expect(response.body).to include(topic1.title)
end
end
context "without search query" do
it "shows search form" do
get search_topics_path
expect(response).to have_http_status(:success)
expect(response.body).to include("Search the PostgreSQL Hackers Archive")
end
end
context "with empty search query" do
it "shows search form" do
get search_topics_path, params: { q: " " }
expect(response).to have_http_status(:success)
expect(response.body).to include("Search the PostgreSQL Hackers Archive")
end
end
context "with no results" do
it "shows no results message" do
get search_topics_path, params: { q: "nonexistent" }
expect(response.body).to include("No results found")
end
end
end
end