2022-05-27 05:15:14 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
|
RSpec.describe UserStatusController do
|
2022-11-24 10:16:28 -05:00
|
|
|
|
describe "#get" do
|
|
|
|
|
it "requires user to be logged in" do
|
|
|
|
|
get "/user-status.json"
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 404 if the feature is disabled" do
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
|
|
|
|
|
|
get "/user-status.json"
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when feature is enabled and a user is logged in" do
|
2023-11-09 17:47:59 -05:00
|
|
|
|
fab!(:user)
|
2022-11-24 10:16:28 -05:00
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns user status" do
|
|
|
|
|
status = "off to dentist"
|
|
|
|
|
status_emoji = "tooth"
|
2023-10-05 14:41:12 -04:00
|
|
|
|
ends_at = "2100-01-01T18:00:00Z"
|
2022-11-24 10:16:28 -05:00
|
|
|
|
user.set_status!(status, status_emoji, DateTime.parse(ends_at))
|
|
|
|
|
|
|
|
|
|
get "/user-status.json"
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.parsed_body["description"]).to eq(status)
|
|
|
|
|
expect(response.parsed_body["emoji"]).to eq(status_emoji)
|
|
|
|
|
expect(response.parsed_body["ends_at"]).to eq(ends_at)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-05-27 05:15:14 -04:00
|
|
|
|
describe "#set" do
|
|
|
|
|
it "requires user to be logged in" do
|
|
|
|
|
put "/user-status.json", params: { description: "off to dentist" }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 404 if the feature is disabled" do
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
|
|
|
|
|
|
put "/user-status.json", params: { description: "off" }
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "feature is enabled and user is logged in" do
|
2023-11-09 17:47:59 -05:00
|
|
|
|
fab!(:user)
|
2022-05-27 05:15:14 -04:00
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-22 10:15:33 -04:00
|
|
|
|
it "the description parameter is mandatory" do
|
|
|
|
|
put "/user-status.json", params: { emoji: "tooth" }
|
|
|
|
|
expect(response.status).to eq(400)
|
2022-05-27 05:15:14 -04:00
|
|
|
|
end
|
|
|
|
|
|
2022-06-22 10:15:33 -04:00
|
|
|
|
it "the emoji parameter is mandatory" do
|
|
|
|
|
put "/user-status.json", params: { description: "off to dentist" }
|
2022-05-27 05:15:14 -04:00
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-27 09:32:27 -05:00
|
|
|
|
it "validates emoji" do
|
2023-01-30 03:49:08 -05:00
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
emoji: "invalid_emoji_name",
|
|
|
|
|
description: "off to dentist",
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(422)
|
2023-01-27 09:32:27 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "limits description’s length" do
|
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
emoji: "tooth",
|
|
|
|
|
description: "x" * UserStatus::MAX_DESCRIPTION_LENGTH,
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
emoji: "tooth",
|
|
|
|
|
description: "x" * (UserStatus::MAX_DESCRIPTION_LENGTH + 1),
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-22 10:15:33 -04:00
|
|
|
|
it "sets user status" do
|
|
|
|
|
status = "off to dentist"
|
|
|
|
|
status_emoji = "tooth"
|
2022-07-05 11:12:22 -04:00
|
|
|
|
ends_at = DateTime.parse("2100-01-01 18:00")
|
|
|
|
|
|
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
description: status,
|
|
|
|
|
emoji: status_emoji,
|
|
|
|
|
ends_at: ends_at,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2022-06-22 10:15:33 -04:00
|
|
|
|
expect(user.user_status.description).to eq(status)
|
|
|
|
|
expect(user.user_status.emoji).to eq(status_emoji)
|
2022-07-05 11:12:22 -04:00
|
|
|
|
expect(user.user_status.ends_at).to eq_time(ends_at)
|
2022-06-22 10:15:33 -04:00
|
|
|
|
end
|
|
|
|
|
|
2022-05-27 05:15:14 -04:00
|
|
|
|
it "following calls update status" do
|
|
|
|
|
status = "off to dentist"
|
2022-06-22 10:15:33 -04:00
|
|
|
|
status_emoji = "tooth"
|
2022-07-05 11:12:22 -04:00
|
|
|
|
ends_at = DateTime.parse("2100-01-01 18:00")
|
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
description: status,
|
|
|
|
|
emoji: status_emoji,
|
|
|
|
|
ends_at: ends_at,
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
2022-05-27 05:15:14 -04:00
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_status.description).to eq(status)
|
2022-06-22 10:15:33 -04:00
|
|
|
|
expect(user.user_status.emoji).to eq(status_emoji)
|
2022-07-05 11:12:22 -04:00
|
|
|
|
expect(user.user_status.ends_at).to eq_time(ends_at)
|
2022-05-27 05:15:14 -04:00
|
|
|
|
|
2022-06-22 10:15:33 -04:00
|
|
|
|
new_status = "surfing"
|
|
|
|
|
new_status_emoji = "surfing_man"
|
2022-07-05 11:12:22 -04:00
|
|
|
|
new_ends_at = DateTime.parse("2100-01-01 18:59")
|
|
|
|
|
put "/user-status.json",
|
|
|
|
|
params: {
|
|
|
|
|
description: new_status,
|
|
|
|
|
emoji: new_status_emoji,
|
|
|
|
|
ends_at: new_ends_at,
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
2022-05-27 05:15:14 -04:00
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_status.description).to eq(new_status)
|
2022-06-22 10:15:33 -04:00
|
|
|
|
expect(user.user_status.emoji).to eq(new_status_emoji)
|
2022-07-05 11:12:22 -04:00
|
|
|
|
expect(user.user_status.ends_at).to eq_time(new_ends_at)
|
2022-05-27 05:15:14 -04:00
|
|
|
|
end
|
2022-05-30 05:41:53 -04:00
|
|
|
|
|
|
|
|
|
it "publishes to message bus" do
|
|
|
|
|
status = "off to dentist"
|
2022-06-22 10:15:33 -04:00
|
|
|
|
emoji = "tooth"
|
2022-07-05 11:12:22 -04:00
|
|
|
|
ends_at = "2100-01-01T18:00:00Z"
|
|
|
|
|
|
2022-06-22 10:15:33 -04:00
|
|
|
|
messages =
|
2023-01-30 17:42:47 -05:00
|
|
|
|
MessageBus.track_publish("/user-status") do
|
2022-07-05 11:12:22 -04:00
|
|
|
|
put "/user-status.json", params: { description: status, emoji: emoji, ends_at: ends_at }
|
2022-06-22 10:15:33 -04:00
|
|
|
|
end
|
2022-05-30 05:41:53 -04:00
|
|
|
|
|
2023-01-30 08:54:44 -05:00
|
|
|
|
expect(messages.map(&:channel)).to contain_exactly("/user-status")
|
2022-07-07 09:37:05 -04:00
|
|
|
|
expect(messages[0].channel).to eq("/user-status")
|
|
|
|
|
expect(messages[0].group_ids).to eq([Group::AUTO_GROUPS[:trust_level_0]])
|
2022-07-05 11:12:22 -04:00
|
|
|
|
|
2022-07-07 09:37:05 -04:00
|
|
|
|
expect(messages[0].data[user.id][:description]).to eq(status)
|
|
|
|
|
expect(messages[0].data[user.id][:emoji]).to eq(emoji)
|
|
|
|
|
expect(messages[0].data[user.id][:ends_at]).to eq(ends_at)
|
2022-05-30 05:41:53 -04:00
|
|
|
|
end
|
2022-05-27 05:15:14 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "#clear" do
|
|
|
|
|
it "requires you to be logged in" do
|
|
|
|
|
delete "/user-status.json"
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns 404 if the feature is disabled" do
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
|
|
|
|
|
|
delete "/user-status.json"
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "feature is enabled and user is logged in" do
|
|
|
|
|
fab!(:user_status) { Fabricate(:user_status, description: "off to dentist") }
|
|
|
|
|
fab!(:user) { Fabricate(:user, user_status: user_status) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "clears user status" do
|
|
|
|
|
delete "/user-status.json"
|
2022-07-05 11:12:22 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2022-05-27 05:15:14 -04:00
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_status).to be_nil
|
|
|
|
|
end
|
2022-05-30 05:41:53 -04:00
|
|
|
|
|
|
|
|
|
it "publishes to message bus" do
|
2023-01-30 17:42:47 -05:00
|
|
|
|
messages = MessageBus.track_publish("/user-status") { delete "/user-status.json" }
|
2022-05-30 05:41:53 -04:00
|
|
|
|
|
|
|
|
|
expect(messages.size).to eq(1)
|
2022-07-07 09:37:05 -04:00
|
|
|
|
expect(messages[0].channel).to eq("/user-status")
|
|
|
|
|
expect(messages[0].group_ids).to eq([Group::AUTO_GROUPS[:trust_level_0]])
|
|
|
|
|
|
|
|
|
|
expect(messages[0].data[user.id]).to eq(nil)
|
2022-05-30 05:41:53 -04:00
|
|
|
|
end
|
2022-05-27 05:15:14 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|