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
per_page: 10, described_class.new(page: 1,
count: 0) } per_page: 10,
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
per_page: 100, described_class.new(page: 5,
count: 1000) } per_page: 100,
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
per_page: 100, described_class.new(page: 10,
count: 1000) } per_page: 100,
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
require 'spec_helper' require 'spec_helper'
describe API::Users, api: true do describe API::Users, api: true do
include ApiHelpers include ApiHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:key) { create(:key, user: user) } let(:key) { create(:key, user: user) }
let(:email) { create(:email, user: user) } let(:email) { create(:email, user: user) }
let(:omniauth_user) { create(:omniauth_user) } let(:omniauth_user) { create(:omniauth_user) }
let(:ldap_user) { create(:omniauth_user, provider: 'ldapmain') } let(:ldap_user) { create(:omniauth_user, provider: 'ldapmain') }
let(:ldap_blocked_user) { create(:omniauth_user, provider: 'ldapmain', state: 'ldap_blocked') } let(:ldap_blocked_user) { create(:omniauth_user, provider: 'ldapmain', state: 'ldap_blocked') }
...@@ -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
...@@ -195,9 +195,9 @@ describe API::Users, api: true do ...@@ -195,9 +195,9 @@ describe API::Users, api: true do
it "does not create user with invalid email" do it "does not create user with invalid email" do
post api('/users', admin), post api('/users', admin),
email: 'invalid email', email: 'invalid email',
password: 'password', password: 'password',
name: 'test' name: 'test'
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
end end
...@@ -223,12 +223,12 @@ describe API::Users, api: true do ...@@ -223,12 +223,12 @@ describe API::Users, api: true do
it 'returns 400 error if user does not validate' do it 'returns 400 error if user does not validate' do
post api('/users', admin), post api('/users', admin),
password: 'pass', password: 'pass',
email: 'test@example.com', email: 'test@example.com',
username: 'test!', username: 'test!',
name: 'test', name: 'test',
bio: 'g' * 256, bio: 'g' * 256,
projects_limit: -1 projects_limit: -1
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']['password']). expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)']) to eq(['is too short (minimum is 8 characters)'])
...@@ -248,19 +248,19 @@ describe API::Users, api: true do ...@@ -248,19 +248,19 @@ describe API::Users, api: true do
context 'with existing user' do context 'with existing user' do
before do before do
post api('/users', admin), post api('/users', admin),
email: 'test@example.com', email: 'test@example.com',
password: 'password', password: 'password',
username: 'test', username: 'test',
name: 'foo' name: 'foo'
end end
it 'returns 409 conflict error if user with same email exists' do it 'returns 409 conflict error if user with same email exists' do
expect do expect do
post api('/users', admin), post api('/users', admin),
name: 'foo', name: 'foo',
email: 'test@example.com', email: 'test@example.com',
password: 'password', password: 'password',
username: 'foo' username: 'foo'
end.to change { User.count }.by(0) end.to change { User.count }.by(0)
expect(response).to have_http_status(409) expect(response).to have_http_status(409)
expect(json_response['message']).to eq('Email has already been taken') expect(json_response['message']).to eq('Email has already been taken')
...@@ -269,10 +269,10 @@ describe API::Users, api: true do ...@@ -269,10 +269,10 @@ describe API::Users, api: true do
it 'returns 409 conflict error if same username exists' do it 'returns 409 conflict error if same username exists' do
expect do expect do
post api('/users', admin), post api('/users', admin),
name: 'foo', name: 'foo',
email: 'foo@example.com', email: 'foo@example.com',
password: 'password', password: 'password',
username: 'test' username: 'test'
end.to change { User.count }.by(0) end.to change { User.count }.by(0)
expect(response).to have_http_status(409) expect(response).to have_http_status(409)
expect(json_response['message']).to eq('Username has already been taken') expect(json_response['message']).to eq('Username has already been taken')
...@@ -382,12 +382,12 @@ describe API::Users, api: true do ...@@ -382,12 +382,12 @@ describe API::Users, api: true do
it 'returns 400 error if user does not validate' do it 'returns 400 error if user does not validate' do
put api("/users/#{user.id}", admin), put api("/users/#{user.id}", admin),
password: 'pass', password: 'pass',
email: 'test@example.com', email: 'test@example.com',
username: 'test!', username: 'test!',
name: 'test', name: 'test',
bio: 'g' * 256, bio: 'g' * 256,
projects_limit: -1 projects_limit: -1
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']['password']). expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)']) to eq(['is too short (minimum is 8 characters)'])
...@@ -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
...@@ -984,7 +984,7 @@ describe API::Users, api: true do ...@@ -984,7 +984,7 @@ describe API::Users, api: true do
end end
describe 'PUT /users/:id/unblock' do describe 'PUT /users/:id/unblock' do
let(:blocked_user) { create(:user, state: 'blocked') } let(:blocked_user) { create(:user, state: 'blocked') }
before { admin } before { admin }
it 'unblocks existing user' do it 'unblocks existing user' do
...@@ -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