forked from grape-oauth2/grape_oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_spec.rb
105 lines (84 loc) · 2.69 KB
/
client_spec.rb
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
require 'spec_helper'
describe 'Grape::OAuth2::Sequel::Client', skip_if: ENV['ORM'] != 'sequel' do
let(:client) { Application.new(name: 'Test') }
let(:key) { SecureRandom.hex(8) }
let(:secret) { SecureRandom.hex(8) }
it 'generates key on create' do
expect(client.key).to be_nil
client.save
expect(client.key).not_to be_nil
end
it 'generates key on create if an empty string' do
client.key = ''
client.save
expect(client.key).not_to be_blank
end
it 'generates key on create unless one is set' do
client.key = key
client.save
expect(client.key).to eq(key)
end
it 'is invalid without key' do
client.save
client.key = nil
expect(client).not_to be_valid
end
it 'checks uniqueness of key' do
app1 = Application.create(name: 'app1')
app2 = Application.create(name: 'app2')
app2.key = app1.key
expect(app2).not_to be_valid
expect(app2.errors).to include(:key)
end
it 'expects database to throw an error when keys are the same' do
app1 = Application.create(name: 'app1')
app2 = Application.create(name: 'app2')
app2.key = app1.key
expect { app2.save }.to raise_error(Sequel::ValidationFailed)
end
it 'generate secret on create' do
expect(client.secret).to be_nil
client.save
expect(client.secret).not_to be_nil
end
it 'generate secret on create if is blank string' do
client.secret = ''
client.save
expect(client.secret).not_to be_blank
end
it 'generate secret on create unless one is set' do
client.secret = secret
client.save
expect(client.secret).to eq(secret)
end
it 'is invalid without secret' do
client.save
client.secret = nil
expect(client).not_to be_valid
end
describe '#authenticate' do
it 'returns a class instance if authenticated successfully' do
client.key = key
client.secret = secret
client.save
expect(Application.authenticate(key, secret)).to eq(client)
end
it 'returns a class instance if only key specified' do
client.key = key
client.save
expect(Application.authenticate(key)).to eq(client)
end
it 'returns nil if authentication failed' do
client.key = key
client.secret = secret
client.save
expect(Application.authenticate(key, 'invalid-')).to be_nil
end
it 'delete all the associated access tokens on destroy' do
user = User.create(username: 'John', password: '123123')
app = Application.create(name: 'app1', redirect_uri: 'https://google.com')
3.times { AccessToken.create(resource_owner_id: user.id, client_id: app.id) }
expect { app.refresh.destroy }.to change { AccessToken.count }.from(3).to(0)
end
end
end