|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Github::Repos::Starring do |
| 6 | + let(:github) { Github.new } |
| 7 | + let(:user) { 'peter-murach' } |
| 8 | + let(:repo) { 'github' } |
| 9 | + |
| 10 | + after { github.user, github.repo, github.oauth_token = nil, nil, nil } |
| 11 | + |
| 12 | + describe "#list" do |
| 13 | + before do |
| 14 | + stub_get("/repos/#{user}/#{repo}/stargazers"). |
| 15 | + to_return(:body => fixture("repos/stargazers.json"), |
| 16 | + :status => 200, :headers => {}) |
| 17 | + end |
| 18 | + |
| 19 | + it "should fail to get resource without username" do |
| 20 | + expect { |
| 21 | + github.repos.starring.list |
| 22 | + }.to raise_error(ArgumentError) |
| 23 | + end |
| 24 | + |
| 25 | + it "should yield iterator if block given" do |
| 26 | + github.repos.starring.should_receive(:list). |
| 27 | + with(user, repo).and_yield('github') |
| 28 | + github.repos.starring.list(user, repo) { |param| 'github' } |
| 29 | + end |
| 30 | + |
| 31 | + it "should get the resources" do |
| 32 | + github.repos.starring.list user, repo |
| 33 | + a_get("/repos/#{user}/#{repo}/stargazers").should have_been_made |
| 34 | + end |
| 35 | + |
| 36 | + it "should return array of resources" do |
| 37 | + stargazers = github.repos.starring.list user, repo |
| 38 | + stargazers.should be_an Array |
| 39 | + stargazers.should have(1).items |
| 40 | + end |
| 41 | + |
| 42 | + it "should return result of mash type" do |
| 43 | + stargazers = github.repos.starring.list user, repo |
| 44 | + stargazers.first.should be_a Hashie::Mash |
| 45 | + end |
| 46 | + |
| 47 | + it "should get watcher information" do |
| 48 | + stargazers = github.repos.starring.list user, repo |
| 49 | + stargazers.first.login.should == 'octocat' |
| 50 | + end |
| 51 | + |
| 52 | + context "fail to find resource" do |
| 53 | + before do |
| 54 | + stub_get("/repos/#{user}/#{repo}/stargazers"). |
| 55 | + to_return(:body => "", :status => 404) |
| 56 | + end |
| 57 | + |
| 58 | + it "should return 404 not found message" do |
| 59 | + expect { |
| 60 | + github.repos.starring.list user, repo |
| 61 | + }.to raise_error(Github::Error::NotFound) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe "#starred" do |
| 67 | + context "if user unauthenticated" do |
| 68 | + it "should fail to get resource without username " do |
| 69 | + stub_get("/user/starred"). |
| 70 | + to_return(:body => '', :status => 401, :headers => {}) |
| 71 | + expect { |
| 72 | + github.repos.starring.starred |
| 73 | + }.to raise_error(Github::Error::Unauthorized) |
| 74 | + end |
| 75 | + |
| 76 | + it "should get the resource with username" do |
| 77 | + stub_get("/users/#{user}/starred"). |
| 78 | + to_return(:body => fixture("repos/starred.json"), :status => 200, :headers => {}) |
| 79 | + github.repos.starring.starred :user => user |
| 80 | + a_get("/users/#{user}/starred").should have_been_made |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "if user authenticated" do |
| 85 | + before do |
| 86 | + github.oauth_token = OAUTH_TOKEN |
| 87 | + stub_get("/user/starred"). |
| 88 | + with(:query => {:access_token => OAUTH_TOKEN}). |
| 89 | + to_return(:body => fixture("repos/starred.json"), |
| 90 | + :status => 200, :headers => {}) |
| 91 | + end |
| 92 | + |
| 93 | + it "should get the resources" do |
| 94 | + github.repos.starring.starred |
| 95 | + a_get("/user/starred").with(:query => {:access_token => OAUTH_TOKEN}). |
| 96 | + should have_been_made |
| 97 | + end |
| 98 | + |
| 99 | + it "should return array of resources" do |
| 100 | + starred = github.repos.starring.starred |
| 101 | + starred.should be_an Array |
| 102 | + starred.should have(1).items |
| 103 | + end |
| 104 | + |
| 105 | + it "should get starred information" do |
| 106 | + starred = github.repos.starring.starred |
| 107 | + starred.first.name.should == 'Hello-World' |
| 108 | + starred.first.owner.login.should == 'octocat' |
| 109 | + end |
| 110 | + end |
| 111 | + end # starred |
| 112 | + |
| 113 | + describe "starring?" do |
| 114 | + context "with username ane reponame passed" do |
| 115 | + context "this repo is being watched by the user" |
| 116 | + before do |
| 117 | + stub_get("/user/starred/#{user}/#{repo}"). |
| 118 | + to_return(:body => "", :status => 404, |
| 119 | + :headers => {:user_agent => github.user_agent}) |
| 120 | + end |
| 121 | + |
| 122 | + it "should return false if resource not found" do |
| 123 | + starring = github.repos.starring.starring? user, repo |
| 124 | + starring.should be_false |
| 125 | + end |
| 126 | + |
| 127 | + it "should return true if resoure found" do |
| 128 | + stub_get("/user/starred/#{user}/#{repo}"). |
| 129 | + to_return(:body => "", :status => 200, |
| 130 | + :headers => {:user_agent => github.user_agent}) |
| 131 | + starring = github.repos.starring.starring? user, repo |
| 132 | + starring.should be_true |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + context "without username and reponame passed" do |
| 137 | + it "should fail validation " do |
| 138 | + expect { |
| 139 | + github.repos.starring.starring?(nil, nil) |
| 140 | + }.to raise_error(ArgumentError) |
| 141 | + end |
| 142 | + end |
| 143 | + end # starring? |
| 144 | + |
| 145 | + describe "#star" do |
| 146 | + context "user authenticated" do |
| 147 | + context "with correct information" do |
| 148 | + before do |
| 149 | + github.oauth_token = OAUTH_TOKEN |
| 150 | + stub_put("/user/starred/#{user}/#{repo}"). |
| 151 | + with(:query => {:access_token => OAUTH_TOKEN}). |
| 152 | + to_return(:body => "", :status => 204, :headers => {}) |
| 153 | + end |
| 154 | + |
| 155 | + it "should successfully star a repo" do |
| 156 | + github.repos.starring.star user, repo |
| 157 | + a_put("/user/starred/#{user}/#{repo}"). |
| 158 | + with(:query => {:access_token => OAUTH_TOKEN}). |
| 159 | + should have_been_made |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context "user unauthenticated" do |
| 165 | + it "should fail" do |
| 166 | + stub_put("/user/starred/#{user}/#{repo}"). |
| 167 | + to_return(:body => "", :status => 401, :headers => {}) |
| 168 | + expect { |
| 169 | + github.repos.starring.star user, repo |
| 170 | + }.to raise_error(Github::Error::Unauthorized) |
| 171 | + end |
| 172 | + end |
| 173 | + end # star |
| 174 | + |
| 175 | + describe "#unstar" do |
| 176 | + context "user authenticated" do |
| 177 | + context "with correct information" do |
| 178 | + before do |
| 179 | + github.oauth_token = OAUTH_TOKEN |
| 180 | + stub_delete("/user/starred/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}"). |
| 181 | + to_return(:body => "", :status => 204, :headers => {}) |
| 182 | + end |
| 183 | + |
| 184 | + it "should successfully unstar a repo" do |
| 185 | + github.repos.starring.unstar user, repo |
| 186 | + a_delete("/user/starred/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}"). |
| 187 | + should have_been_made |
| 188 | + end |
| 189 | + end |
| 190 | + end |
| 191 | + |
| 192 | + context "user unauthenticated" do |
| 193 | + it "should fail" do |
| 194 | + stub_delete("/user/starred/#{user}/#{repo}"). |
| 195 | + to_return(:body => "", :status => 401, :headers => {}) |
| 196 | + expect { |
| 197 | + github.repos.starring.unstar user, repo |
| 198 | + }.to raise_error(Github::Error::Unauthorized) |
| 199 | + end |
| 200 | + end |
| 201 | + end # stop_watching |
| 202 | + |
| 203 | +end # Github::Respos::Starring |
0 commit comments