Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump rack from 2.2.3 to 2.2.6.4 #1277

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ GEM
multi_json (~> 1.0)
pusher-signature (~> 0.1.8)
pusher-signature (0.1.8)
rack (2.2.3)
rack (2.2.6.4)
rack-attack (5.4.2)
rack (>= 1.0, < 3)
rack-contrib (2.2.0)
Expand Down
17 changes: 9 additions & 8 deletions lib/travis/api/app/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
class Travis::Api::App
class AccessToken
DEFAULT_SCOPES = [:public, :private]
attr_reader :token, :scopes, :user_id, :app_id, :expires_in, :extra
attr_reader :token, :travis_token, :scopes, :user_id, :app_id, :expires_in, :extra

def self.create(options = {})
new(options).tap(&:save)
end

def self.for_travis_token(travis_token, options = {})
travis_token = Token.find_by_token(travis_token) unless travis_token.respond_to? :user
new(scope: :travis_token, app_id: 1, user: travis_token.user).tap(&:save) if travis_token
new(scope: :travis_token, app_id: 1, user: travis_token.user, travis_token: travis_token).tap(&:save) if travis_token
end

def self.find_by_token(token)
Expand All @@ -32,12 +32,13 @@ def initialize(options = {})
raise ArgumentError, 'expires_in must be of integer type'
end

@app_id = Integer(options[:app_id])
@scopes = Array(options[:scopes] || options[:scope] || DEFAULT_SCOPES).map(&:to_sym)
@user = options[:user]
@user_id = Integer(options[:user_id] || @user.id)
@token = options[:token] || reuse_token || SecureRandom.urlsafe_base64(16)
@extra = options[:extra]
@app_id = Integer(options[:app_id])
@scopes = Array(options[:scopes] || options[:scope] || DEFAULT_SCOPES).map(&:to_sym)
@user = options[:user]
@user_id = Integer(options[:user_id] || @user.id)
@token = options[:token] || reuse_token || SecureRandom.urlsafe_base64(16)
@travis_token = options[:travis_token]
@extra = options[:extra]
end

def save
Expand Down
6 changes: 5 additions & 1 deletion lib/travis/api/app/endpoint/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ def update_first_login(user)

def serialize_user(user)
rendered = Travis::Api::Serialize.data(user, version: :v2)
rendered['user'].merge('token' => user.tokens.first.try(:token).to_s)
token = user.tokens.asset.first.try(:token).to_s
rendered['user'].merge(
'token' => token,
'rss_token' => user.tokens.rss.first.try(:token) || token,
)
end

def oauth_endpoint
Expand Down
2 changes: 2 additions & 0 deletions lib/travis/api/app/endpoint/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class RepoStatus < Endpoint
end

get '/:owner_name/:name/builds', scope: [:public, :travis_token] do
halt 401 if scope == :travis_token && access_token.travis_token && !access_token.travis_token.rss? && access_token.user.tokens.rss.exists?

respond_with service(:find_builds, params), responder: :atom, responders: :atom
end

Expand Down
2 changes: 2 additions & 0 deletions lib/travis/model/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# one) that they need use on their service hooks. This gives us some security
# that people cannot throw random repositories at Travis CI.
class Token < Travis::Model
enum purpose: [ :asset, :rss ]

belongs_to :user

validates :token, :presence => true
Expand Down
7 changes: 4 additions & 3 deletions lib/travis/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class User < Travis::Model
has_many :custom_keys, as: :owner

before_create :set_as_recent
after_create :create_a_token
after_create :create_the_tokens
before_save :track_previous_changes

serialize :github_scopes
Expand Down Expand Up @@ -164,8 +164,9 @@ def inspect
github_oauth_token ? super.gsub(github_oauth_token, '[REDACTED]') : super
end

def create_a_token
self.tokens.create!
def create_the_tokens
self.tokens.asset.create! unless self.tokens.asset.exists?
self.tokens.rss.create!
end

def github?
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/model/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,13 @@ def user(payload)
end
end
end

describe 'tokens' do
let(:user) { FactoryBot.create(:user) }

it 'creates two tokens on creation' do
expect(user.tokens.asset.count).to eq(1)
expect(user.tokens.rss.count).to eq(1)
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@
token = described_class.find_by_token(token.token)
expect(token.extra).to eq({ 'required_params' => { 'job_id' => '1' } })
end

it 'allows to save travis token' do
attrs = {
app_id: 1,
user_id: 3,
travis_token: Token.new
}

token = described_class.new(attrs).tap(&:save)
expect(token.travis_token).to eq(attrs[:travis_token])
end
end
35 changes: 35 additions & 0 deletions spec/unit/endpoint/repos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,39 @@
end
end
end

describe 'builds endpoint' do
let(:user) { FactoryBot.create(:user) }
let(:repo) { FactoryBot.create(:repository, private: false, owner_name: 'user', name: 'repo') }

before { user.permissions.create(repository_id: repo.id, push: false) }

context 'when user is authorizing with token' do
context 'and token is not a RSS one' do
let(:token) { user.tokens.asset.first }

context 'and user has a RSS token' do
it 'responds with 401' do
expect(get("/repo_status/#{repo.owner_name}/#{repo.name}/builds.atom?token=#{token.token}", {}, {}).status).to eq(401)
end
end

context 'and user does not have a RSS token' do
before { user.tokens.rss.delete_all }

it 'responds with 200' do
expect(get("/repo_status/#{repo.owner_name}/#{repo.name}/builds.atom?token=#{token.token}", {}, {}).status).to eq(200)
end
end
end

context 'and token is a RSS one' do
let(:token) { user.tokens.rss.first }

it 'responds with 200' do
expect(get("/repo_status/#{repo.owner_name}/#{repo.name}/builds.atom?token=#{token.token}", {}, {}).status).to eq(200)
end
end
end
end
end