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'
describe Gitlab::PaginationDelegate, lib: true do
context 'no data' do
let(:delegate) { described_class.new(page: 1,
per_page: 10,
count: 0) }
let(:delegate) do
described_class.new(page: 1,
per_page: 10,
count: 0)
end
it 'shows the correct total count' do
expect(delegate.total_count).to eq(0)
......@@ -44,9 +46,11 @@ describe Gitlab::PaginationDelegate, lib: true do
end
context 'with data' do
let(:delegate) { described_class.new(page: 5,
per_page: 100,
count: 1000) }
let(:delegate) do
described_class.new(page: 5,
per_page: 100,
count: 1000)
end
it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000)
......@@ -86,9 +90,11 @@ describe Gitlab::PaginationDelegate, lib: true do
end
context 'last page' do
let(:delegate) { described_class.new(page: 10,
per_page: 100,
count: 1000) }
let(:delegate) do
described_class.new(page: 10,
per_page: 100,
count: 1000)
end
it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000)
......
......@@ -11,11 +11,11 @@ describe Gitlab::PaginationUtil, lib: true do
context 'class with no pagination delegate defined' do
let(:pagination_class) { Class.new { extend Gitlab::PaginationUtil } }
let(:pagination_delegate) {
let(:pagination_delegate) do
Gitlab::PaginationDelegate.new(page: 1,
per_page: 10,
count: 20)
}
end
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'
describe API::Users, api: true do
describe API::Users, api: true do
include ApiHelpers
let(:user) { create(:user) }
let(:user) { create(:user) }
let(:admin) { create(:admin) }
let(:key) { create(:key, user: user) }
let(:email) { create(:email, user: user) }
let(:key) { create(:key, user: user) }
let(:email) { create(:email, user: user) }
let(:omniauth_user) { create(:omniauth_user) }
let(:ldap_user) { create(:omniauth_user, provider: 'ldapmain') }
let(:ldap_blocked_user) { create(:omniauth_user, provider: 'ldapmain', state: 'ldap_blocked') }
......@@ -131,7 +131,7 @@ describe API::Users, api: true do
end
describe "POST /users" do
before{ admin }
before { admin }
it "creates user" do
expect do
......@@ -195,9 +195,9 @@ describe API::Users, api: true do
it "does not create user with invalid email" do
post api('/users', admin),
email: 'invalid email',
password: 'password',
name: 'test'
email: 'invalid email',
password: 'password',
name: 'test'
expect(response).to have_http_status(400)
end
......@@ -223,12 +223,12 @@ describe API::Users, api: true do
it 'returns 400 error if user does not validate' do
post api('/users', admin),
password: 'pass',
email: 'test@example.com',
username: 'test!',
name: 'test',
bio: 'g' * 256,
projects_limit: -1
password: 'pass',
email: 'test@example.com',
username: 'test!',
name: 'test',
bio: 'g' * 256,
projects_limit: -1
expect(response).to have_http_status(400)
expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)'])
......@@ -248,19 +248,19 @@ describe API::Users, api: true do
context 'with existing user' do
before do
post api('/users', admin),
email: 'test@example.com',
password: 'password',
username: 'test',
name: 'foo'
email: 'test@example.com',
password: 'password',
username: 'test',
name: 'foo'
end
it 'returns 409 conflict error if user with same email exists' do
expect do
post api('/users', admin),
name: 'foo',
email: 'test@example.com',
password: 'password',
username: 'foo'
name: 'foo',
email: 'test@example.com',
password: 'password',
username: 'foo'
end.to change { User.count }.by(0)
expect(response).to have_http_status(409)
expect(json_response['message']).to eq('Email has already been taken')
......@@ -269,10 +269,10 @@ describe API::Users, api: true do
it 'returns 409 conflict error if same username exists' do
expect do
post api('/users', admin),
name: 'foo',
email: 'foo@example.com',
password: 'password',
username: 'test'
name: 'foo',
email: 'foo@example.com',
password: 'password',
username: 'test'
end.to change { User.count }.by(0)
expect(response).to have_http_status(409)
expect(json_response['message']).to eq('Username has already been taken')
......@@ -382,12 +382,12 @@ describe API::Users, api: true do
it 'returns 400 error if user does not validate' do
put api("/users/#{user.id}", admin),
password: 'pass',
email: 'test@example.com',
username: 'test!',
name: 'test',
bio: 'g' * 256,
projects_limit: -1
password: 'pass',
email: 'test@example.com',
username: 'test!',
name: 'test',
bio: 'g' * 256,
projects_limit: -1
expect(response).to have_http_status(400)
expect(json_response['message']['password']).
to eq(['is too short (minimum is 8 characters)'])
......@@ -454,7 +454,7 @@ describe API::Users, api: true do
key_attrs = attributes_for :key
expect do
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
it "returns 400 for invalid ID" do
......@@ -541,7 +541,7 @@ describe API::Users, api: true do
email_attrs = attributes_for :email
expect do
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
it "returns a 400 for invalid ID" do
......@@ -792,7 +792,7 @@ describe API::Users, api: true do
key_attrs = attributes_for :key
expect do
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)
end
......@@ -827,7 +827,7 @@ describe API::Users, api: true do
user.save
expect do
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)
end
......@@ -908,7 +908,7 @@ describe API::Users, api: true do
email_attrs = attributes_for :email
expect do
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)
end
......@@ -931,7 +931,7 @@ describe API::Users, api: true do
user.save
expect do
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)
end
......@@ -984,7 +984,7 @@ describe API::Users, api: true do
end
describe 'PUT /users/:id/unblock' do
let(:blocked_user) { create(:user, state: 'blocked') }
let(:blocked_user) { create(:user, state: 'blocked') }
before { admin }
it 'unblocks existing user' do
......@@ -1106,15 +1106,69 @@ describe API::Users, api: true do
let(:request) { get api("/user/activities", admin) }
end
context 'last activities' do
it 'returns the last activities' do
context 'last activity as normal user' 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
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
......
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