-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoauth_spec.rb
More file actions
141 lines (111 loc) · 3.86 KB
/
oauth_spec.rb
File metadata and controls
141 lines (111 loc) · 3.86 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
139
140
141
# encoding: utf-8
require 'spec_helper'
describe 'OAuth' do
OAUTH_INITIATE_OPTS = {
client_id: "client_id",
redirect_uri: "http://myapp.com/callback",
scope: "master+releases",
}.freeze
OAUTH_CHECKTOKEN_OPTS = {
grant_type: "authorization_code",
code: "thecode",
redirect_uri: "http://myapp.com/callback",
client_id: "client_id",
client_secret: "client_secret",
}
describe "Public API" do
PUBLIC_URL = "https://micro.prismic.io/api"
it "should accept unauthenticated requests" do
expect {
Prismic.api(PUBLIC_URL, nil)
}.not_to raise_error()
end
describe "oauth_initiate_url" do
it "should returns the URL" do
url = Prismic::API.oauth_initiate_url(PUBLIC_URL, OAUTH_INITIATE_OPTS)
expected_url = [
"https://micro.prismic.io/auth?client_id=client_id",
"redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
"scope=master%2Breleases",
].join("&")
url.should == expected_url
end
it "should returns the URL (with access_token)" do
url = Prismic.oauth_initiate_url(PUBLIC_URL, OAUTH_INITIATE_OPTS, "token")
expected_url = [
"https://micro.prismic.io/auth?client_id=client_id",
"redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
"scope=master%2Breleases",
].join("&")
url.should == expected_url
end
end
describe "oauth_check_token" do
it "should returns the URL" do
expect {
Prismic.oauth_check_token(PUBLIC_URL, OAUTH_CHECKTOKEN_OPTS)
}.to raise_error(
# we don't want to get a PrismicWSAuthError error here
Prismic::API::PrismicWSConnectionError,
"Can't connect to Prismic's API: 400 Bad Request"
)
end
end
end
describe "Private API" do
PRIVATE_URL = "https://privaterepository.prismic.io/api"
it "should deny unauthenticated requests" do
expect {
Prismic.api(PRIVATE_URL, nil)
}.to raise_error(Prismic::API::PrismicWSAuthError)
end
def catch_wserror(url)
Prismic.api(url, nil)
rescue Prismic::API::PrismicWSAuthError => e
e
end
it "should provide the error" do
error = catch_wserror(PRIVATE_URL)
error.message.should == "Can't connect to Prismic's API: "
end
it "should provide oauth entry points" do
error = catch_wserror(PRIVATE_URL)
error.oauth.initiate.should == "https://privaterepository.prismic.io/auth"
end
it "should provide oauth token" do
error = catch_wserror(PRIVATE_URL)
error.oauth.token.should == "https://privaterepository.prismic.io/auth/token"
end
describe "oauth_initiate_url" do
it "should returns the URL" do
url = Prismic.oauth_initiate_url(PRIVATE_URL, OAUTH_INITIATE_OPTS)
expected_url = [
"https://privaterepository.prismic.io/auth?client_id=client_id",
"redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
"scope=master%2Breleases",
].join("&")
url.should == expected_url
end
it "should returns the URL (with access_token)" do
url = Prismic.oauth_initiate_url(PRIVATE_URL, OAUTH_INITIATE_OPTS, "token")
expected_url = [
"https://privaterepository.prismic.io/auth?client_id=client_id",
"redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
"scope=master%2Breleases",
].join("&")
url.should == expected_url
end
end
describe "oauth_check_token" do
it "should returns the URL" do
expect {
Prismic::API.oauth_check_token(PRIVATE_URL, OAUTH_CHECKTOKEN_OPTS)
}.to raise_error(
# we don't want to get a PrismicWSAuthError error here
Prismic::API::PrismicWSConnectionError,
"Can't connect to Prismic's API: 400 Bad Request"
)
end
end
end
end