-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add Asset and Organization resources #10
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6078d4
Add Asset and Organization resources
jeffgran-dox 3577b67
require ostruct
jeffgran-dox 6818b41
Use older ruby syntax for more compatibility
jeffgran-dox 2021299
Updates from CR feedback
jeffgran-dox 30bdda1
add asset spec
jeffgran-dox 623dcf7
add program.structured_scopes spec
jeffgran-dox 915dddd
Fix cross-contaminated specs
jeffgran-dox f1899cc
Update spec/spec_helper.rb
jeffgran-dox 25dbdec
Update spec/hackerone/client/asset_spec.rb
jeffgran-dox 5f7ac10
Update lib/hackerone/client/asset.rb
jeffgran-dox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module HackerOne | ||
module Client | ||
class Asset | ||
include ResourceHelper | ||
|
||
DELEGATES = [ | ||
:asset_type, | ||
:identifier, | ||
:description, | ||
:coverage, | ||
:max_severity, | ||
:confidentiality_requirement, | ||
:integrity_requirement, | ||
:availability_requirement, | ||
:created_at, | ||
:updated_at, | ||
:archived_at, | ||
:reference, | ||
:state, | ||
] | ||
|
||
delegate *DELEGATES, to: :attributes | ||
|
||
attr_reader :organization | ||
|
||
def initialize(asset, organization) | ||
@asset = asset | ||
@organization = organization | ||
end | ||
|
||
def id | ||
@asset[:id] | ||
end | ||
|
||
def update(attributes:) | ||
body = { | ||
type: "asset", | ||
attributes: attributes | ||
} | ||
make_put_request("organizations/#{organization.id}/assets/#{id}", request_body: body) | ||
end | ||
|
||
def programs | ||
relationships.programs[:data].map { |p| Program.new(p) } | ||
end | ||
|
||
private | ||
|
||
def relationships | ||
OpenStruct.new(@asset[:relationships]) | ||
end | ||
|
||
def attributes | ||
OpenStruct.new(@asset[:attributes]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
rzhade3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module HackerOne | ||
module Client | ||
class Organization | ||
include ResourceHelper | ||
|
||
delegate :handle, :created_at, :updated_at, to: :attributes | ||
|
||
def initialize(org) | ||
@organization = org | ||
end | ||
|
||
def id | ||
@organization[:id] | ||
end | ||
|
||
def assets(page_number: 1, page_size: 100) | ||
make_get_request( | ||
"organizations/#{id}/assets", | ||
params: { page: { number: page_number, size: page_size } } | ||
).map do |asset_data| | ||
Asset.new(asset_data, self) | ||
end | ||
end | ||
|
||
private | ||
|
||
def attributes | ||
OpenStruct.new(@organization[:attributes]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe HackerOne::Client::Asset do | ||
before(:all) do | ||
ENV["HACKERONE_TOKEN_NAME"] = "foo" | ||
ENV["HACKERONE_TOKEN"] = "bar" | ||
end | ||
before(:each) do | ||
stub_request(:get, "https://api.hackerone.com/v1/programs/18969"). | ||
to_return(body: <<~JSON) | ||
{ | ||
"data": { | ||
"id": "18969", | ||
"type": "program", | ||
"attributes": { | ||
"handle": "github", | ||
"created_at": "2016-02-02T04:05:06.000Z", | ||
"updated_at": "2016-02-02T04:05:06.000Z" | ||
}, | ||
"relationships": { | ||
"organization": { | ||
"data": { | ||
"id": "14", | ||
"type": "organization", | ||
"attributes": { | ||
"handle": "api-example", | ||
"created_at": "2016-02-02T04:05:06.000Z", | ||
"updated_at": "2016-02-02T04:05:06.000Z" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
JSON | ||
|
||
stub_request(:get, "https://api.hackerone.com/v1/organizations/14/assets?page%5Bnumber%5D=1&page%5Bsize%5D=100"). | ||
to_return(body: <<~JSON2) | ||
{ | ||
"data": [ | ||
{ | ||
"id": "2", | ||
"type": "asset", | ||
"attributes": { | ||
"asset_type": "domain", | ||
"domain_name": "hackerone.com", | ||
"description": null, | ||
"coverage": "untested", | ||
"max_severity": "critical", | ||
"confidentiality_requirement": "high", | ||
"integrity_requirement": "high", | ||
"availability_requirement": "high", | ||
"created_at": "2016-02-02T04:05:06.000Z", | ||
"updated_at": "2016-02-02T04:05:06.000Z", | ||
"archived_at": "2017-02-02T04:05:06.000Z", | ||
"reference": "reference", | ||
"state": "confirmed" | ||
}, | ||
"relationships": { | ||
"asset_tags": { | ||
"data": [ | ||
{ | ||
"id": "1", | ||
"type": "asset-tag", | ||
"attributes": { | ||
"name": "test" | ||
}, | ||
"relationships": { | ||
"asset_tag_category": { | ||
"data": { | ||
"id": "2", | ||
"type": "asset-tag-category", | ||
"attributes": { | ||
"name": "test" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"programs": { | ||
"data": [ | ||
{ | ||
"id": "18969", | ||
"type": "program", | ||
"attributes": { | ||
"handle": "github", | ||
"name": "team name" | ||
} | ||
} | ||
] | ||
}, | ||
"attachments": { | ||
"data": [ | ||
{ | ||
"id": "1337", | ||
"type": "attachment", | ||
"attributes": { | ||
"expiring_url": "https://attachments.s3.amazonaws.com/G74PuDP6qdEdN2rpKNLkVwZF", | ||
"created_at": "2016-02-02T04:05:06.000Z", | ||
"file_name": "example.png", | ||
"content_type": "image/png", | ||
"file_size": 16115 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"links": {} | ||
} | ||
JSON2 | ||
end | ||
|
||
after(:each) do | ||
# clear cached programs to prevent contaminatin between tests | ||
HackerOne::Client::Program.instance_variable_set(:@my_programs, nil) | ||
end | ||
|
||
let(:program) do | ||
VCR.use_cassette(:programs) do | ||
HackerOne::Client::Program.find("github") | ||
end | ||
end | ||
|
||
let(:organization) do | ||
program.organization | ||
end | ||
|
||
let(:assets) do | ||
organization.assets | ||
end | ||
|
||
let(:asset) { assets[0] } | ||
|
||
it "returns a collection" do | ||
expect(assets).to be_kind_of(Array) | ||
expect(assets.size).to eq(1) | ||
end | ||
|
||
it "returns id" do | ||
expect(asset.id).to be_present | ||
expect(asset.id).to eq("2") | ||
end | ||
|
||
it "returns organization" do | ||
expect(asset.organization).to be_present | ||
expect(asset.organization.id).to eq("14") | ||
end | ||
|
||
it "returns programs" do | ||
expect(asset.programs).to be_kind_of(Array) | ||
expect(asset.programs.first.id).to eq("18969") | ||
end | ||
|
||
it "updates the asset" do | ||
req = stub_request(:put, "https://api.hackerone.com/v1/organizations/14/assets/2"). | ||
with { |r| | ||
r.body == <<~BODY.strip | ||
{"data":{"type":"asset","attributes":{"description":"This is the new description"}}} | ||
BODY | ||
}. | ||
to_return(body: "{}") # we are not using the response for now so not bothering to stub it properly | ||
|
||
asset.update( | ||
attributes: { | ||
description: "This is the new description" | ||
} | ||
) | ||
|
||
expect(req).to have_been_requested | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed, as Ostruct is bundled with Ruby: https://github.com/ruby/ostruct?tab=readme-ov-file#installation.
I don't see anything inherently wrong with this, so fine with the diff but it in theory it shouldn't be breaking anything currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't need this when I was testing this locally but then when I tried to use it in another context it crashed with an "openstruct is not defined" error. I'm not sure what the cause was, maybe a different ruby version that doesn't require it by default any more or something. So I feel like it's worth leaving it in to prevent someone else from running into that, and like you say I think it should be harmless.