Skip to content

Commit

Permalink
Merge pull request #12 from amratab/authentication_support
Browse files Browse the repository at this point in the history
Authentication support
  • Loading branch information
luaybs committed Apr 14, 2016
2 parents 4000de5 + 4c7cd99 commit ab17933
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ In your test:
StripeTester.stripe_version = "2015-10-16"
```

4. Send the webhook. This will send a POST request to the URL with the event data as JSON:
4. If you are using username and password in your stripe webhook event, you can provide it in two ways.
```ruby
StripeTester.webhook_password = "<password>"
or
you can set it in your webhook_url itself like this
# Normal HTTP URL
StripeTester.webhook_url = "http://stripe:[email protected]/my_post_url"

# HTTPS URL
StripeTester.webhook_url = "https://stripe:[email protected]/my_post_url"
```

5. Send the webhook. This will send a POST request to the URL with the event data as JSON:
```ruby
# as a symbol
StripeTester.create_event(:invoice_created)
Expand Down
14 changes: 13 additions & 1 deletion lib/stripe_tester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.post_to_url(data={})
req = Net::HTTP::Post.new(post_url.path)
req.content_type = 'application/json'
req.body = data.to_json

req.basic_auth 'stripe', self.basic_authentication_password if !self.basic_authentication_password.nil?
http_object = Net::HTTP.new(post_url.hostname, post_url.port)
http_object.use_ssl = true if post_url.scheme == 'https'
http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE if (!verify_ssl? && http_object.use_ssl?)
Expand Down Expand Up @@ -125,6 +125,18 @@ def self.remove_url
@url = nil
end

def self.webhook_password=(webhook_password)
@webhook_password = webhook_password
end

def self.webhook_password
@webhook_password
end

def self.basic_authentication_password
@webhook_password || self.webhook_url.password
end

def self.stripe_version=(version)
@version = version
end
Expand Down
50 changes: 50 additions & 0 deletions spec/stripe_tester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
after(:each) do
StripeTester.stripe_version = nil
StripeTester.verify_ssl = nil
StripeTester.webhook_password = nil
end

describe "#load_template" do
Expand Down Expand Up @@ -74,6 +75,23 @@
expect(result_url.to_s).to eq(url)
end

it "#webhook_url should set the correct url if authentication is provided in url itself" do
url = 'http://abc:[email protected]'
StripeTester.webhook_url = url

result_url = StripeTester.webhook_url
expect(result_url.to_s).to eq(url)
end

it "#webhook_url should have correct url when password is provided through webhook_password" do
url = 'http://www.google.com'
StripeTester.webhook_url = url
StripeTester.webhook_password = 'password'

result_url = StripeTester.webhook_url
expect(result_url.to_s).to eq(url)
end

it "#verify_ssl should default to true" do
result_verify = StripeTester.verify_ssl?
expect(result_verify).to eq(true)
Expand Down Expand Up @@ -114,6 +132,38 @@
expect(response).to be_truthy
end

it "#post_to_url should return true when authentication is provided" do
data = StripeTester.load_template(:invoice_created)
url = "http://localhost:3000/transactions"
fake_web_url = "http://stripe:password@localhost:3000/transactions"
StripeTester.webhook_url = url
StripeTester.webhook_password='password'

FakeWeb.register_uri(:post,
fake_web_url,
body: data.to_json,
content_type: 'application/json')

response = StripeTester.post_to_url(data)

expect(response).to be_truthy
end

it "#post_to_url should return true when authentication is provided through url" do
data = StripeTester.load_template(:invoice_created)
url = "http://stripe:password@localhost:3000/transactions"
StripeTester.webhook_url = url

FakeWeb.register_uri(:post,
url,
body: data.to_json,
content_type: 'application/json')

response = StripeTester.post_to_url(data)

expect(response).to be_truthy
end

it "#post_to_url should raise an error when request fails" do
data = StripeTester.load_template(:invoice_created)
url = "http://localhost:3000/"
Expand Down

0 comments on commit ab17933

Please sign in to comment.