forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavatar_helper_spec.rb
More file actions
138 lines (115 loc) · 4.92 KB
/
Copy pathavatar_helper_spec.rb
File metadata and controls
138 lines (115 loc) · 4.92 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../sharding_spec_helper.rb')
describe AvatarHelper do
include AvatarHelper
context "avatars" do
let_once(:user) {user_model(short_name: "test user")}
let(:services) {{avatars: true}}
let(:avatar_size) {50}
let(:request) { ActionDispatch::Request.new(Rack::MockRequest.env_for("http://test.host/")) }
def service_enabled?(type)
services[type]
end
describe ".avatar_image_attrs" do
it "accepts a user id" do
self.expects(:avatar_url_for_user).with(user).returns("test_url")
expect(avatar_image_attrs(user.id)).to eq ["test_url", user.short_name]
end
it "accepts a user" do
self.expects(:avatar_url_for_user).with(user).returns("test_url")
expect(avatar_image_attrs(user)).to eq ["test_url", user.short_name]
end
it "falls back to blank avatar when given a user id of 0" do
expect(avatar_image_attrs(0)).to eq ["/images/messages/avatar-50.png", '']
end
it "falls back to blank avatar when user's avatar has been reported during this session" do
self.expects(:session).at_least_once.returns({"reported_#{user.id}" => true})
expect(avatar_image_attrs(user)).to eq ["/images/messages/avatar-50.png", '']
end
it "falls back to a blank avatar when the user is nil" do
expect(avatar_image_attrs(nil)).to eq ["/images/messages/avatar-50.png", '']
end
end
describe ".avatar" do
let_once(:user) {user_model}
it "leaves off the href if url is nil" do
expect(avatar(user, url: nil)).not_to match(/href/)
end
it "sets the href to the given url" do
expect(avatar(user, url: "/test_url")).to match(/href="\/test_url"/)
end
it "links to the context user's page when given a context_code" do
self.expects(:context_prefix).with('course_1').returns('/courses/1')
expect(avatar(user, context_code: "course_1")).to match("href=\"/courses/1/users/#{user.id}\"")
end
it "links to the user's page" do
expect(avatar(user)).to match("/users/#{user.id}")
end
it "falls back to a blank avatar when the user is nil" do
expect(avatar(nil)).to match("/images/messages/avatar-50.png")
end
end
context "with avatar service off" do
let(:services) {{avatars: false}}
it "should return full URIs for users" do
expect(avatar_url_for_user(user)).to match(%r{\Ahttps?://})
expect(avatar_url_for_user(user, true)).to match(%r{\Ahttps?://})
end
end
it "should return full URIs for users" do
user_factory
expect(avatar_url_for_user(@user)).to match(%r{\Ahttps?://})
expect(avatar_url_for_user(@user, true)).to match(%r{\Ahttps?://})
@user.account.set_service_availability(:avatars, true)
@user.avatar_image_source = 'no_pic'
@user.save!
# reload to clear instance vars
@user = User.find(@user.id)
expect(avatar_url_for_user(@user)).to match(%r{\Ahttps?://})
expect(avatar_url_for_user(@user, true)).to match(%r{\Ahttps?://})
@user.avatar_state = 'approved'
@user.avatar_image_source = 'attachment'
@user.avatar_image_url = "/relative/canvas/path"
@user.save!
@user = User.find(@user.id)
expect(avatar_url_for_user(@user)).to eq "http://test.host/relative/canvas/path"
@user.avatar_image_source = 'external'
@user.avatar_image_url = "http://www.example.com/path"
@user.save!
@user = User.find(@user.id)
expect(avatar_url_for_user(@user)).to eq "http://www.example.com/path"
end
it "should return full URIs for groups" do
expect(avatar_url_for_group).to match(%r{\Ahttps?://})
expect(avatar_url_for_group(true)).to match(%r{\Ahttps?://})
end
context "from other shard" do
specs_require_sharding
it "should return full path across shards" do
@user.account.set_service_availability(:avatars, true)
@user.avatar_image_source = 'attachment'
@user.avatar_image_url = "/relative/canvas/path"
@shard2.activate do
expect(avatar_url_for_user(@user)).to eq "http://test.host/relative/canvas/path"
end
end
end
end
end