Running Pact against test environment in Rails API - ruby-on-rails

Just playing around with Pact against my Rails API and noticed that the out-of-the-box Pact setup runs against the "development" environment by default.
How do I configure to run against the "test" environment without having to specify it in command line when I run the task (RAILS_ENV=test). Couldn't find it easily in the docs how to do it.
Using following gems:
pact (1.10.0)
pact-mock_service (0.12.1)
pact-support (0.6.0)
pact_helper.rb:
require 'pact/provider/rspec'
Pact.service_provider 'Auslan API Service' do
honours_pact_with 'Auslan Web App' do
# This example points to a local file, however, on a real project with a continuous
# integration box, you would use a [Pact Broker](https://github.com/bethesque/pact_broker) or publish your pacts as artifacts,
# and point the pact_uri to the pact published by the last successful build.
pact_uri './user-specs-user-api.json' # need to update this
end
end
Pact.configure do | config |
config.diff_formatter = :embedded
end
Pact.provider_states_for 'User-Specs' do
provider_state 'there are users already added inside the database' do
set_up do
user1 = User.create(email: 'abcd#a.au', first_name: 'Jane', last_name: 'Doe', password: 'abcd#1234')
# set the Auth token
token = Knock::AuthToken.new(payload: { sub: user1.id }).token
pacts = File.join(File.dirname(File.expand_path(__FILE__)), '../../user-specs-user-api.json')
Dir.glob(pacts).each do |f|
text = File.read(f)
output_of_gsub = text.gsub(/\"Authorization\"\s*:\s*\".+\"/) { "\"Authorization\": \"Bearer #{token}\"" }
File.open(f, "w") { |file| file.puts output_of_gsub }
end
end
end
end
Thanks,
Mo

I haven't written any code to allow that to happen. The part of the code where the app gets loaded is here: https://github.com/pact-foundation/pact-ruby/blob/master/lib/pact/provider/configuration/service_provider_dsl.rb#L16
You can override the app in the configuration if you have a handle to it, but I can't remember how to do that with with a Rails app off the top of my head. Maybe you could have a play around with the Rack builder and see if you can pass in any environment variables to it. I'd be happy to accept a PR if you can work out how to do it.

Related

Ruby Google Cloud PubSub async pull returns GRPC::DeadlineExceeded error

Hi I have a subscription set up in Google PubSub and I am trying to pull messages asynchronously using the "official" google-cloud-ruby library. Here is my code which will be executed from a rake task which passes in subscription_name:
def pull!
creds = Google::Cloud::PubSub::Credentials.new(
GCP_CREDENTIALS_KEYFILE_PATH,
scope: "https://www.googleapis.com/auth/pubsub"
)
messages = []
pubsub = Google::Cloud::PubSub.new(
project_id: GOOGLE_PROJECT_ID,
credentials: creds
)
subscription = pubsub.subscription(subscription_name)
subscription.pull(immediate: true).each do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
messages.push(received_message)
end
# Return the collected messages
messages
rescue => error
Rails.logger error
messages.presence
end
The Google::Cloud::PubSub::Credentials part references a working keyfile. I know the JSON keyfile is good since I can use it to generate a working Bearer token using oauth2l and pull from the PubSub using cURL, postman, Net::HTTP, etc. Using the same JSON credentials for a separate Google::Cloud::Storage service and that works fine also.
But for some reason using Google::Cloud::PubSub it just hangs and won't respond. After about 60 seconds I get the following error:
GRPC::DeadlineExceeded: 4:Deadline Exceeded. debug_error_string:{"created":"#1602610740.445195000","description":"Deadline Exceeded","file":"src/core/ext/filters/deadline/deadline_filter.cc","file_line":69,"grpc_status":4}
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:29:in `check_status'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:180:in `attach_status_results_and_complete_call'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:376:in `request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/client_stub.rb:172:in `block (2 levels) in request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/interceptors.rb:170:in `intercept!'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/client_stub.rb:171:in `block in request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/gapic-common-0.3.4/lib/gapic/grpc/service_stub/rpc_call.rb:121:in `call'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/gapic-common-0.3.4/lib/gapic/grpc/service_stub.rb:156:in `call_rpc'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-v1-0.1.2/lib/google/cloud/pubsub/v1/subscriber/client.rb:503:in `get_subscription'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-2.1.0/lib/google/cloud/pubsub/service.rb:154:in `get_subscription'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-2.1.0/lib/google/cloud/pubsub/project.rb:286:in `subscription'
Adding a debugger shows that the following line is what hangs and results in an error:
subscription = pubsub.subscription(subscription_name)
I've tried everything I can think of based on the documentation. Updated all related gems and even tried using deprecated syntax out of desparation. If anyone has at least an idea of where to start that would be most appreciated, thanks!
UPDATE
So it turns out that for some reason running this locally was not able to connect, but when shipped to a deployed environment the above code connected flawlessly to pubsub and was able to pull and ack messages. Further, upon making the initial connection in the deployed environment, I am now able to connect locally as well using the same credentials.
Link to Github issue conversation for context around the troubleshooting process and quartzmo suggestion to try deploying to another environment.
Are you still having this issue? I just tried to reproduce it using Ruby 2.6.5p114, google-cloud-pubsub 2.1.0 and grpc 1.32.0 (same versions as you), but I cannot reproduce it. Here is my code (slightly modified to run in a Minitest spec context) for comparison:
GCP_CREDENTIALS_KEYFILE_PATH = "/Users/quartzmo/my-project.json"
GOOGLE_PROJECT_ID = "my-project-id"
def pull! topic_name, subscription_name
creds = Google::Cloud::PubSub::Credentials.new(
GCP_CREDENTIALS_KEYFILE_PATH,
scope: "https://www.googleapis.com/auth/pubsub"
)
messages = []
pubsub = Google::Cloud::PubSub.new(
project_id: GOOGLE_PROJECT_ID,
credentials: creds
)
topic = pubsub.create_topic topic_name
topic.subscribe subscription_name
topic.publish "A test message from #{topic_name} to #{subscription_name}"
subscription = pubsub.subscription(subscription_name)
subscription.pull(immediate: true).each do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
messages.push(received_message)
end
# Return the collected messages
messages
end
focus
it "pull!" do
topic_name = random_topic_name
subscription_name = random_subscription_name
messages = pull! topic_name, subscription_name
assert_equal 1, messages.count
assert_equal "A test message from #{topic_name} to #{subscription_name}", messages[0].data
end
And this is the output:
% bundle exec rake test
Run options: --junit --junit-filename=sponge_log.xml --seed 30984
# Running:
Received message: A test message from ruby-pubsub-samples-test-topic-7cb10bde to ruby-pubsub-samples-test-subscription-f47f2eaa
.
Finished in 6.529219s, 0.1532 runs/s, 0.3063 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
Update (2020-10-20): This issue was resolved when executing the code in a different environment, although the reason is unknown. See comment on GitHub issue.

Configuring Ruby On Rails Application With Iex-Ruby-Client Gem

I am a beginner programmer. I recently built an application that uses the iex-ruby-client gem to pull stock quotes for me that I enter into a webpage form. It worked perfectly.
However, in early June, IEX changed their API so that you have to have a publishable token from the IEX cloud console. I got my publishable token from IEX cloud console.
The updated gem docs (https://github.com/dblock/iex-ruby-client) say that I have to "Configure" the application now. I simply don't know how or where I would implement the configuration code. Here is the suggested code from the gem documentation. I just don't know where to put it.
Configure IEX::Api.configure do |config|
config.publishable_token = 'token' # defaults to
ENV['IEX_API_PUBLISHABLE_TOKEN']
config.endpoint = 'https://sandbox.iexapis.com/v1' # defaults to
'https://cloud.iexapis.com/v1'
end
The documents also state, "You can also configure an instance of a client directly."
client = IEX::Api::Client.new(
publishable_token: 'token',
endpoint: 'https://sandbox.iexapis.com/v1'
)
I am adding extra code to clarify what I have done based on the response here. Here is my new config/initializers/iex-ruby-client.rb file (token info isn't the real one).
IEX::Api.configure do |config|
config.publishable_token = 'pk_3b38fsdadfsafjsdalfjdsakfjlda12f519'
config.endpoint = 'https://sandbox.iexapis.com/v1'
end
Here is the relevant method in the controller where I require the library:
def index
require 'iex-ruby-client'
if params[:id] == ""
#nothing = "You forgot to enter a symbol ;)."
elsif
if params[:id]
begin
#stock = IEX::Resources::Quote.get(params[:id])
#company = IEX::Resources::Company.get(params[:id])
rescue StandardError
#error = "That stock symbol doesn't seem to exist. Please enter
another symbol."
end
end
end
end
So I have created the config file and required the gem at the top of the method, but I am still getting an error. I'm sure there is some flaw in my implementation of this token requirement. If you have any additional suggestions, I welcome them. But if this is too much to ask on Stack Overflow, I understand. Thanks.
Well, you clearly have two choices:
use initializer by creating a config file(i.e: iex_client.rb) under the directory /config/initializers and add:
Configure IEX::Api.configure do |config|
config.publishable_token = 'token' # defaults to
ENV['IEX_API_PUBLISHABLE_TOKEN']
config.endpoint = 'https://sandbox.iexapis.com/v1' # defaults to
'https://cloud.iexapis.com/v1'
end
just use the client object wherever you want like this:
client = IEX::Api::Client.new(
publishable_token: 'token',
endpoint: 'https://sandbox.iexapis.com/v1'
)
You probably need to replace token with a correct one. You also need to make sure to require the library wherever you wanna use it.
After unsuccessfully attempting to configure the IEX-ruby-client gem (as described in my question here on stack overflow), I switched over to the stock_quote gem. That gem is built off of the same IEX API, and I had no problems configuring the app with a stock_quote.rb file saved inside config/initializers.

How to use Rails Low Level Cache in production?

I have an application which calls an API and authenticates using a token. I need to store this token and refresh it every so often, so I am using Rails.cache.fetch to do so in a custom class for handling the API calls. This works great on my local dev machine, but in production it is erroring. I am running a Mac for my dev machine and production is on Ubuntu 18. Here is the code that raises the error:
def authenticate
Rails.cache.fetch(#token, expires_in: 2.hours.to_i) do
login_uri = #base_uri + "auth/login"
auth_response = HTTParty.post(login_uri, body: { username: ENV["API_USERNAME"], password: ENV["API_PASSWORD"] } )
#token = auth_response.parsed_response["token"]
end
end
Here is the error I am getting:
Errno::ENOTDIR (Not a directory # rb_file_s_rename -
(/var/www/myapp/releases/20190424134348/tmp/cache/.00020190424-1954-lgpvq5,
/var/www/myapp/releases/20190424134348/tmp/cache/001/000/)):
It looks like Rails is attempting to rename or move the cache file for some reason. Looking at the server the /001 directory is there, but the subdirectory /000 does not exist.

rails rspec capybara cannot get my internal api to connect

Constructing a basic rails app I'm re-factoring to do heavy lifting on an external docker/compute as a service i.e. iron.io. the 'worker'
In refactoring created Grape API to allow status of processing from remote 'worker' to notify the server when processing is done. The user interface then uses ajax to poll the local server to update. API and basic tests all ok. It also works in development using Delayed::job running the worker.
I however cannot seem to get my capybara tests to work end to end as the delayed::job running process making the HTTP request back to the server always gets connection refused.
It works fine if i run a rails server in parallel as the tests: (RAILS_ENV="test" rails s -p 3001), then make sure the ENV variable is set to port 3001.
I had tried
various combination of Capybara.configure (as below)
in the test: visit url (where url="http://#{Capybara.server_host}:#{Capybara.server_port}" ) to see if that 'kicks off' the server perhaps
various webdrivers (poltergeist, selenium etc)
Any thoughts, experience or guidance much appreciated
Ben
note: in the code
populate the domain & port via ENV[''] variables that are populated (these environment variables will be set in the running environment iron.io)
port & app_host set as below
ENV variables populated in the test
Capybara.configure do |config|
config.run_server = true
config.server_port = "9876"
config.app_host = "http://127.0.0.1:9876"
end
rails 4.1.0
rspec 3.4.0
capybara 2.7.0
poltergeist 1.5.1
selenium 2.53.0
I think you're trying to have your test too too much. I would recommend that you "mock out" the interactions with the other service to make the tests self sufficient. In the past I have added a test.js that:
Mocks out ajax on the page
Checks for specific requests to have been made (page.evaluate_script)
Responds back to them in the way your external service will (execute_script)
Like this:
# test.js
$.ajax = function(settings) {
window.__ajaxRequests || (window.__ajaxRequests = []);
window.__ajaxRequests.push(settings);
return {
done: function(cb) { settings.__done = cb; }
}
}
# spec/features/jobs_spec.rb
visit '/jobs'
click_button 'Start job'
requests = page.evaulate_script('window.__ajaxRequests')
expect(requests.size).to eq(1)
expect(requests[0].url).to eq('http://jobs.yourproduct.com/start')
...
expect(page).not_to have_content('Job completed')
page.execute_script('window.__ajaxRequests[0].__done({data:{status:"complete"}})')
expect(page).to have_content('Job completed')

Playing around with mails in Rails

I`m trying to create the following feature: You register and receive an email like vouldjeff+ewr#myapp.com and when you send something to this email it automatically appears in something like your wall... So my problem is how to realize the creation of the email and the receiving of the mail itself.
Any ideas?
Ruby provides Net/IMAP and Net/POP3 you can use to login into your email account.
Here's a small tutorial.
POP3
pop = Net::POP3.new("pop.gmail.com", port)
pop.enable_ssl
pop.start('YourAccount', 'YourPassword')
if pop.mails.empty?
puts 'No mail.'
else
i = 0
pop.each_mail do |m|
File.open("inbox/#{i}", 'w') do |f|
f.write m.pop
end
m.delete
i += 1
end
puts "#{pop.mails.size} mails popped."
end
pop.finish
IMAP
imap = Net::IMAP.new('imap.gmail.com')
imap.authenticate('LOGIN', 'username', 'password')
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
MailReader.receive(msg)
imap.store(message_id, "+FLAGS", [:Deleted])
end
imap.expunge()
There might be other options but that's how we do it:
Postfix
Rails Cron Job
Postfix allows you to specify a MySQL table/view to check whether an email address exists or not. You can also define Mail Forwardings.
Create a DB View to match the requirements on Postfix
This View should contain all the email addresses and forward them to a different mail account, like mailparser.
Now your Rails can either
use a POP3/IMAP frontend to the mailserver (you should install Dovecot or Courier then) to fetch the mails and process them
or go to the place on the disk where all the mails are located (check Postfix config for that) and parse the files as TMail objects and process them.
A different option is to make Postfix call script/runner with the Mail data, but rails boot-up can take long and a lot of memory, so I prefer having a Cronjob/Backgroundjob/Worker to do this.
P.S. The Creation of the E-Mail will be done by creating a Model for your Rails app which the View will use as a basis.
Sending E-Mails is simple as pie. Simply have a look at the ActionMailer Basics. If you also want to receive E-Mail, you should write a daemon that fetches Mails from the mailserver continuously in the background.
Here a snippet that fetches Mails via POP:
require 'net/pop'
config = {
:host => "mail.example.com",
:user => "foobar#example.com",
:password => "…",
:port => 110,
:timeout => 10
}
pop = Net::POP3.new(config[:host])
pop.start(config[:user], config[:password])
if pop.mails.empty?
puts "No mails…"
else
pop.mails.each do |mail|
# do stuff with mail
end
end
This is pure Ruby-Code, Rails is not needed for this snippet.

Resources