Commit d9894f25 authored by James Lopez's avatar James Lopez

updated and added specs for activity, etc

parent 9aa64965
...@@ -2,9 +2,11 @@ require 'spec_helper' ...@@ -2,9 +2,11 @@ require 'spec_helper'
describe Gitlab::PaginationDelegate, lib: true do describe Gitlab::PaginationDelegate, lib: true do
context 'no data' do context 'no data' do
let(:delegate) { described_class.new(page: 1, let(:delegate) do
described_class.new(page: 1,
per_page: 10, per_page: 10,
count: 0) } count: 0)
end
it 'shows the correct total count' do it 'shows the correct total count' do
expect(delegate.total_count).to eq(0) expect(delegate.total_count).to eq(0)
...@@ -44,9 +46,11 @@ describe Gitlab::PaginationDelegate, lib: true do ...@@ -44,9 +46,11 @@ describe Gitlab::PaginationDelegate, lib: true do
end end
context 'with data' do context 'with data' do
let(:delegate) { described_class.new(page: 5, let(:delegate) do
described_class.new(page: 5,
per_page: 100, per_page: 100,
count: 1000) } count: 1000)
end
it 'shows the correct total count' do it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000) expect(delegate.total_count).to eq(1000)
...@@ -86,9 +90,11 @@ describe Gitlab::PaginationDelegate, lib: true do ...@@ -86,9 +90,11 @@ describe Gitlab::PaginationDelegate, lib: true do
end end
context 'last page' do context 'last page' do
let(:delegate) { described_class.new(page: 10, let(:delegate) do
described_class.new(page: 10,
per_page: 100, per_page: 100,
count: 1000) } count: 1000)
end
it 'shows the correct total count' do it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000) expect(delegate.total_count).to eq(1000)
......
...@@ -11,11 +11,11 @@ describe Gitlab::PaginationUtil, lib: true do ...@@ -11,11 +11,11 @@ describe Gitlab::PaginationUtil, lib: true do
context 'class with no pagination delegate defined' do context 'class with no pagination delegate defined' do
let(:pagination_class) { Class.new { extend Gitlab::PaginationUtil } } let(:pagination_class) { Class.new { extend Gitlab::PaginationUtil } }
let(:pagination_delegate) { let(:pagination_delegate) do
Gitlab::PaginationDelegate.new(page: 1, Gitlab::PaginationDelegate.new(page: 1,
per_page: 10, per_page: 10,
count: 20) count: 20)
} end
let(:delegated_methods) { %i[total_count total_pages current_page limit_value first_page? prev_page last_page? next_page] } let(:delegated_methods) { %i[total_count total_pages current_page limit_value first_page? prev_page last_page? next_page] }
......
require 'spec_helper'
describe Gitlab::User::Activity, :redis, lib: true do
let(:username) { 'user' }
let(:activity) { described_class.new('user', Time.new(2016, 12, 12).to_i) }
it 'has the username' do
expect(activity.username).to eq(username)
end
it 'has the last activity at' do
expect(activity.last_activity_at).to eq('2016-12-12 00:00:00')
end
end
...@@ -131,7 +131,7 @@ describe API::Users, api: true do ...@@ -131,7 +131,7 @@ describe API::Users, api: true do
end end
describe "POST /users" do describe "POST /users" do
before{ admin } before { admin }
it "creates user" do it "creates user" do
expect do expect do
...@@ -454,7 +454,7 @@ describe API::Users, api: true do ...@@ -454,7 +454,7 @@ describe API::Users, api: true do
key_attrs = attributes_for :key key_attrs = attributes_for :key
expect do expect do
post api("/users/#{user.id}/keys", admin), key_attrs post api("/users/#{user.id}/keys", admin), key_attrs
end.to change{ user.keys.count }.by(1) end.to change { user.keys.count }.by(1)
end end
it "returns 400 for invalid ID" do it "returns 400 for invalid ID" do
...@@ -541,7 +541,7 @@ describe API::Users, api: true do ...@@ -541,7 +541,7 @@ describe API::Users, api: true do
email_attrs = attributes_for :email email_attrs = attributes_for :email
expect do expect do
post api("/users/#{user.id}/emails", admin), email_attrs post api("/users/#{user.id}/emails", admin), email_attrs
end.to change{ user.emails.count }.by(1) end.to change { user.emails.count }.by(1)
end end
it "returns a 400 for invalid ID" do it "returns a 400 for invalid ID" do
...@@ -792,7 +792,7 @@ describe API::Users, api: true do ...@@ -792,7 +792,7 @@ describe API::Users, api: true do
key_attrs = attributes_for :key key_attrs = attributes_for :key
expect do expect do
post api("/user/keys", user), key_attrs post api("/user/keys", user), key_attrs
end.to change{ user.keys.count }.by(1) end.to change { user.keys.count }.by(1)
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
end end
...@@ -827,7 +827,7 @@ describe API::Users, api: true do ...@@ -827,7 +827,7 @@ describe API::Users, api: true do
user.save user.save
expect do expect do
delete api("/user/keys/#{key.id}", user) delete api("/user/keys/#{key.id}", user)
end.to change{user.keys.count}.by(-1) end.to change { user.keys.count }.by(-1)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
...@@ -908,7 +908,7 @@ describe API::Users, api: true do ...@@ -908,7 +908,7 @@ describe API::Users, api: true do
email_attrs = attributes_for :email email_attrs = attributes_for :email
expect do expect do
post api("/user/emails", user), email_attrs post api("/user/emails", user), email_attrs
end.to change{ user.emails.count }.by(1) end.to change { user.emails.count }.by(1)
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
end end
...@@ -931,7 +931,7 @@ describe API::Users, api: true do ...@@ -931,7 +931,7 @@ describe API::Users, api: true do
user.save user.save
expect do expect do
delete api("/user/emails/#{email.id}", user) delete api("/user/emails/#{email.id}", user)
end.to change{user.emails.count}.by(-1) end.to change { user.emails.count }.by(-1)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
...@@ -1106,15 +1106,69 @@ describe API::Users, api: true do ...@@ -1106,15 +1106,69 @@ describe API::Users, api: true do
let(:request) { get api("/user/activities", admin) } let(:request) { get api("/user/activities", admin) }
end end
context 'last activities' do context 'last activity as normal user' do
it 'returns the last activities' do it 'has no permission' do
user.record_activity
get api("/user/activities", user)
expect(response).to have_http_status(404)
end
end
context 'last activity as admin' do
it 'returns the last activity' do
user.record_activity user.record_activity
get api("/user/activities", admin) get api("/user/activities", admin)
activity = json_response activity = json_response.first
expect(activity['username']).to eq(user.username)
expect(activity['lat_activity_at']).to start_with(Date.today.year.to_s)
end
end
context 'last activities paginated' do
let(:activity) { json_response.first }
let(:old_date) { 1.year.ago.to_date }
before do
5.times do |num|
Timecop.freeze(old_date + num)
create(:user, username: num.to_s).record_activity
end
end
after do
Timecop.return
end
it 'returns 3 activities' do
get api("/user/activities?page=1&per_page=3", admin)
expect(json_response.count).to eq(3)
end
it 'contains the first activities' do
get api("/user/activities?page=1&per_page=3", admin)
expect(json_response.map { |activity| activity['username'] }).to eq(%w[0 1 2])
end
it 'contains the last activities' do
get api("/user/activities?page=2&per_page=3", admin)
expect(json_response.map { |activity| activity['username'] }).to eq(%w[3 4])
end
it 'contains activities created after user 3 was created' do
from = (old_date + 3).to_s("%Y-%m-%d")
get api("/user/activities?page=1&per_page=5&from=#{from}", admin)
expect(activity).to eq('') expect(json_response.map { |activity| activity['username'] }).to eq(%w[3 4])
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment