diff options
author | James Lopez <james@jameslopez.es> | 2016-10-05 16:41:32 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-04-14 15:20:55 +0200 |
commit | 2951a8543ef97ceb1bcaca5f5140d822729c950b (patch) | |
tree | ff02540e813cd93e8df8a438c9b22cdacc96982e /spec | |
parent | b61199ce0ccdfcd11a338778ce300cd15e5f2a43 (diff) | |
download | gitlab-ce-2951a8543ef97ceb1bcaca5f5140d822729c950b.tar.gz |
Add user activity service and spec. Also added relevant - NOT offline - migration
It uses a user activity table instead of a column in users.
Tested with mySQL and postgreSQL
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/sessions_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/factories/user_activities.rb | 6 | ||||
-rw-r--r-- | spec/requests/api/internal_spec.rb | 13 | ||||
-rw-r--r-- | spec/requests/git_http_spec.rb | 6 | ||||
-rw-r--r-- | spec/services/event_create_service_spec.rb | 13 | ||||
-rw-r--r-- | spec/services/users/activity_service_spec.rb | 26 |
6 files changed, 70 insertions, 0 deletions
diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 9c16a7bc08b..3459f30ef42 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -37,6 +37,12 @@ describe SessionsController do subject.sign_out user end end + + it 'updates the user activity' do + expect do + post(:create, user: { login: user.username, password: user.password }) + end.to change { user.reload.last_activity_at }.from(nil) + end end end diff --git a/spec/factories/user_activities.rb b/spec/factories/user_activities.rb new file mode 100644 index 00000000000..32ad8c6a3b2 --- /dev/null +++ b/spec/factories/user_activities.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :user_activity do + last_activity_at { Time.now } + user + end +end diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index 4be67df5a00..63f566da7a8 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -151,6 +151,11 @@ describe API::Internal, api: true do context "access granted" do before do project.team << [user, :developer] + Timecop.freeze + end + + after do + Timecop.return end context 'with env passed as a JSON' do @@ -176,6 +181,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.wiki.repository.path_to_repo) + expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) end end @@ -186,6 +192,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.wiki.repository.path_to_repo) + expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) end end @@ -196,6 +203,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.repository.path_to_repo) + expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) end end @@ -206,6 +214,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.repository.path_to_repo) + expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) end context 'project as /namespace/project' do @@ -241,6 +250,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey + expect(key.user.reload.last_activity_at).to be_nil end end @@ -250,6 +260,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey + expect(key.user.reload.last_activity_at).to be_nil end end end @@ -267,6 +278,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey + expect(key.user.reload.last_activity_at).to be_nil end end @@ -276,6 +288,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey + expect(key.user.reload.last_activity_at).to be_nil end end end diff --git a/spec/requests/git_http_spec.rb b/spec/requests/git_http_spec.rb index 006d6a6af1c..9f2857ce2e7 100644 --- a/spec/requests/git_http_spec.rb +++ b/spec/requests/git_http_spec.rb @@ -157,6 +157,12 @@ describe 'Git HTTP requests', lib: true do expect(response).to have_http_status(:ok) end end + + it 'updates the user last activity' do + download(path, env) do |response| + expect(user.reload.last_activity_at).not_to be_nil + end + end end context 'but only project members are allowed' do diff --git a/spec/services/event_create_service_spec.rb b/spec/services/event_create_service_spec.rb index f2c2009bcbf..54e5c0b236b 100644 --- a/spec/services/event_create_service_spec.rb +++ b/spec/services/event_create_service_spec.rb @@ -129,4 +129,17 @@ describe EventCreateService, services: true do it { expect { subject }.to change { Event.count }.from(0).to(1) } end end + + describe '#push' do + let(:project) { create(:empty_project) } + let(:user) { create(:user) } + + it 'creates a new event' do + expect { service.push(project, user, {}) }.to change { Event.count } + end + + it 'updates user last activity' do + expect { service.push(project, user, {}) }.to change { user.last_activity_at } + end + end end diff --git a/spec/services/users/activity_service_spec.rb b/spec/services/users/activity_service_spec.rb new file mode 100644 index 00000000000..68399118579 --- /dev/null +++ b/spec/services/users/activity_service_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Users::ActivityService, services: true do + let(:user) { create(:user) } + subject(:service) { described_class.new(user, 'type') } + + describe '#execute' do + context 'when last activity is nil' do + it 'sets the last activity timestamp' do + service.execute + + expect(user.last_activity_at).not_to be_nil + end + end + + context 'when activity_at is not nil' do + it 'updates the activity multiple times' do + activity = create(:user_activity, user: user) + + Timecop.travel(activity.last_activity_at + 1.minute) do + expect { service.execute }.to change { user.reload.last_activity_at } + end + end + end + end +end |