2016-11-25 03:45:15 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe "Groups" do
|
2016-12-07 04:28:43 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2016-11-29 03:25:02 -05:00
|
|
|
|
2016-12-07 04:28:43 -05:00
|
|
|
def sign_in(user)
|
|
|
|
password = 'somecomplicatedpassword'
|
|
|
|
user.update!(password: password)
|
|
|
|
Fabricate(:email_token, confirmed: true, user: user)
|
2016-11-29 03:25:02 -05:00
|
|
|
post "/session.json", { login: user.username, password: password }
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2016-11-25 03:45:15 -05:00
|
|
|
describe "checking if a group can be mentioned" do
|
|
|
|
let(:group) { Fabricate(:group, name: 'test', users: [user]) }
|
|
|
|
|
|
|
|
it "should return the right response" do
|
2016-12-07 04:28:43 -05:00
|
|
|
sign_in(user)
|
2016-11-25 03:45:15 -05:00
|
|
|
group
|
|
|
|
|
|
|
|
get "/groups/test/mentionable.json", { name: group.name }
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["mentionable"]).to eq(false)
|
|
|
|
|
|
|
|
group.update_attributes!(alias_level: Group::ALIAS_LEVELS[:everyone])
|
|
|
|
|
|
|
|
get "/groups/test/mentionable.json", { name: group.name }
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["mentionable"]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-11-29 03:25:02 -05:00
|
|
|
|
|
|
|
describe "group can be updated" do
|
|
|
|
let(:group) { Fabricate(:group, name: 'test', users: [user]) }
|
|
|
|
|
|
|
|
context "when user is group owner" do
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
2016-12-07 04:28:43 -05:00
|
|
|
sign_in(user)
|
2016-11-29 03:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able update the group" do
|
|
|
|
xhr :put, "/groups/#{group.id}", { group: {
|
|
|
|
flair_bg_color: 'FFF',
|
|
|
|
flair_color: 'BBB',
|
2016-12-05 03:18:24 -05:00
|
|
|
flair_url: 'fa-adjust',
|
2016-12-06 21:26:28 -05:00
|
|
|
bio_raw: 'testing',
|
|
|
|
title: 'awesome team'
|
2016-11-29 03:25:02 -05:00
|
|
|
} }
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
group.reload
|
|
|
|
|
|
|
|
expect(group.flair_bg_color).to eq('FFF')
|
|
|
|
expect(group.flair_color).to eq('BBB')
|
|
|
|
expect(group.flair_url).to eq('fa-adjust')
|
2016-12-05 03:18:24 -05:00
|
|
|
expect(group.bio_raw).to eq('testing')
|
2016-12-06 21:26:28 -05:00
|
|
|
expect(group.title).to eq('awesome team')
|
2016-11-29 03:25:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is group admin" do
|
|
|
|
before do
|
|
|
|
user.update_attributes!(admin: true)
|
2016-12-07 04:28:43 -05:00
|
|
|
sign_in(user)
|
2016-11-29 03:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to update the group' do
|
|
|
|
xhr :put, "/groups/#{group.id}", { group: { flair_color: 'BBB' } }
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(group.reload.flair_color).to eq('BBB')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not a group owner or admin" do
|
|
|
|
it 'should not be able to update the group' do
|
2016-12-07 04:28:43 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
2016-11-29 03:25:02 -05:00
|
|
|
xhr :put, "/groups/#{group.id}", { group: { name: 'testing' } }
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 04:28:43 -05:00
|
|
|
|
|
|
|
describe 'members' do
|
|
|
|
let(:user1) do
|
|
|
|
Fabricate(:user,
|
|
|
|
last_seen_at: Time.zone.now,
|
|
|
|
last_posted_at: Time.zone.now - 1.day,
|
|
|
|
email: 'b@test.org'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user2) do
|
|
|
|
Fabricate(:user,
|
|
|
|
last_seen_at: Time.zone.now - 1 .day,
|
|
|
|
last_posted_at: Time.zone.now,
|
|
|
|
email: 'a@test.org'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:group) { Fabricate(:group, users: [user1, user2]) }
|
|
|
|
|
|
|
|
it "should allow members to be sorted by" do
|
2016-12-08 01:26:50 -05:00
|
|
|
xhr :get, "/groups/#{group.name}/members", order: 'last_seen_at', desc: true
|
2016-12-07 04:28:43 -05:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m["id"] }).to eq([user1.id, user2.id])
|
|
|
|
|
|
|
|
xhr :get, "/groups/#{group.name}/members", order: 'last_seen_at'
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m["id"] }).to eq([user2.id, user1.id])
|
|
|
|
|
2016-12-08 01:26:50 -05:00
|
|
|
xhr :get, "/groups/#{group.name}/members", order: 'last_posted_at', desc: true
|
2016-12-07 04:28:43 -05:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m["id"] }).to eq([user2.id, user1.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow members to be sorted by columns that are not allowed" do
|
|
|
|
xhr :get, "/groups/#{group.name}/members", order: 'email'
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m["id"] }).to eq([user1.id, user2.id])
|
|
|
|
end
|
|
|
|
end
|
2016-11-25 03:45:15 -05:00
|
|
|
end
|