-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathusers_controller_spec.rb
More file actions
98 lines (77 loc) · 2.46 KB
/
users_controller_spec.rb
File metadata and controls
98 lines (77 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
require 'spec_helper'
describe UsersController, type: :controller do
describe 'GET #index' do
let(:subject) { get :index }
it 'renders index template' do
expect(subject).to render_template :index
end
it 'returns 200 status code' do
expect(subject.status).to eq 200
end
it 'assigns @users' do
subject
expect(assigns[:users].name).to eq 'User'
end
end
describe '#show' do
let(:user) { create(:user) }
let(:subject) { get(:show, params: { nickname: user.nickname }) }
context 'when logged in' do
login_user
context 'when user found' do
context 'when viewing own page' do
before { allow(user).to receive(:id).and_return(@current_user.id) }
it 'renders show template' do
expect(subject).to render_template :show
end
it 'returns 200 status code' do
expect(subject.status).to eq 200
end
it 'assigns @user' do
subject
expect(assigns[:user].name).to eq 'kd'
end
it 'assigns @user_tips' do
subject
expect(assigns[:user_tips].name).to eq 'Tip'
end
it 'assigns @recent_tips' do
subject
expect(assigns[:recent_tips].class).to eq Array
end
end
context 'when viewing other\'s page' do
let(:new_user) { create(:user) }
let(:subject) { get(:show, params: { id: new_user.id }) }
it 'redirect to root_path' do
expect(subject).to redirect_to root_path
end
it 'sets flash error message' do
subject
expect(flash[:error]).to eq('You are not authorized to perform this action!')
end
end
end
context 'when user not found' do
let(:subject) { get(:show, params: { nickname: 'unknown-user' }) }
it 'redirect to users_path' do
expect(subject).to redirect_to users_path
end
it 'sets flash error message' do
subject
expect(flash[:error]).to eq('User not found')
end
end
end
context 'when not logged in' do
it 'redirects to login page' do
expect(subject).to redirect_to new_user_session_path
end
it 'sets flash alert message' do
subject
expect(flash[:alert]).to eq('You need to sign in or sign up before continuing.')
end
end
end
end