/
ghe_spec.rb
98 lines (70 loc) · 2.93 KB
/
ghe_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
describe '...' do
MOCK_GHE_HOST = 'ghe.example.com'
MOCK_GHE_PROTOCOL = 'http'
MOCK_USER = 'foo'
MOCK_PASSWORD = 'bar'
MOCK_AUTHZ_GHE_URL = "#{MOCK_GHE_PROTOCOL}://#{MOCK_GHE_HOST}/api/v3/"
MOCK_GHE_URL = "#{MOCK_GHE_PROTOCOL}://#{MOCK_GHE_HOST}/api/v3/"
MOCK_GITHUB_URL = "https://api.github.com/"
MOCK_LOGIN_URL = "https://github.com/"
before do
@saved_env = ENV[Gist::URL_ENV_NAME]
# stub requests for /gists
stub_request(:post, /#{MOCK_GHE_URL}gists/).to_return(:body => %[{"html_url": "http://#{MOCK_GHE_HOST}"}])
stub_request(:post, /#{MOCK_GITHUB_URL}gists/).to_return(:body => '{"html_url": "http://github.com/"}')
# stub requests for /authorizations
stub_request(:post, /#{MOCK_AUTHZ_GHE_URL}authorizations/).
to_return(:status => 201, :body => '{"token": "asdf"}')
stub_request(:post, /#{MOCK_GITHUB_URL}authorizations/).
with(headers: {'Authorization'=>'Basic Zm9vOmJhcg=='}).
to_return(:status => 201, :body => '{"token": "asdf"}')
stub_request(:post, /#{MOCK_LOGIN_URL}login\/device\/code/).
to_return(:status => 200, :body => '{"interval": "0.1", "user_code":"XXXX-XXXX", "device_code": "xxxx", "verification_uri": "https://github.com/login/device"}')
stub_request(:post, /#{MOCK_LOGIN_URL}login\/oauth\/access_token/).
to_return(:status => 200, :body => '{"access_token":"zzzz"}')
end
after do
ENV[Gist::URL_ENV_NAME] = @saved_env
end
describe :login! do
before do
@saved_stdin = $stdin
# stdin emulation
$stdin = StringIO.new "#{MOCK_USER}\n#{MOCK_PASSWORD}\n"
# intercept for updating ~/.gist
File.stub(:open)
end
after do
$stdin = @saved_stdin
end
it "should access to api.github.com when $#{Gist::URL_ENV_NAME} wasn't set" do
ENV.delete Gist::URL_ENV_NAME
Gist.login!
assert_requested(:post, /#{MOCK_LOGIN_URL}login\/oauth\/access_token/)
end
it "should access to #{MOCK_GHE_HOST} when $#{Gist::URL_ENV_NAME} was set" do
ENV[Gist::URL_ENV_NAME] = MOCK_GHE_URL
Gist.login!
assert_requested(:post, /#{MOCK_AUTHZ_GHE_URL}authorizations/)
end
context "when credentials are passed in" do
it "uses them" do
$stdin = StringIO.new "#{MOCK_USER}_wrong\n#{MOCK_PASSWORD}_wrong\n"
Gist.login! :username => MOCK_USER, :password => MOCK_PASSWORD
assert_requested(:post, /#{MOCK_GITHUB_URL}authorizations/)
end
end
end
describe :gist do
it "should access to api.github.com when $#{Gist::URL_ENV_NAME} wasn't set" do
ENV.delete Gist::URL_ENV_NAME
Gist.gist "test gist"
assert_requested(:post, /#{MOCK_GITHUB_URL}gists/)
end
it "should access to #{MOCK_GHE_HOST} when $#{Gist::URL_ENV_NAME} was set" do
ENV[Gist::URL_ENV_NAME] = MOCK_GHE_URL
Gist.gist "test gist"
assert_requested(:post, /#{MOCK_GHE_URL}gists/)
end
end
end