'Confirmation token is invalid' when testing with Cucumber - ruby-on-rails

I'm trying to get a sign up form working with Devise; I'm testing it with Cucumber.
When a user signs up, I send a confirmation e-mail. Something goes wrong when running my test, though.
This is my scenario:
Scenario: Signing in via confirmation
Given there are the following users:
| email | password | unconfirmed |
| user#ticketee.com | password | true |
And "user#ticketee.com" opens the email with subject "Confirmation instructions"
And they click the first link in the email
Then I should see "Your account was successfully confirmed."
And I should see "Signed in as user#ticketee.com"
However, I get the following error:
expected there to be content "Your account was successfully confirmed. You are now signed in." in "\n Ticketee\n \n\nTicketee\nSign up\n Sign in\nResend confirmation instructions\n\n\n \n 1 error prohibited this user from being saved:\n Confirmation token is invalid\n\n\n Email\n\n \n\n Sign inSign upForgot your password?" (RSpec::Expectations::ExpectationNotMetError)
I guess the important part here is:
1 error prohibited this user from being saved:\n Confirmation token is invalid
I have tested this manually (went to sign up and clicked the confirmation link in the e-mail) and this works fine.. It's only when I test through Cucumber that I get the 'Confirmation token is invalid' message. Anyone know how I can fix this? I'd like to see my tests pass..
Thanks a lot.
EDIT: Steps asked for in the comments:
When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
open_email(address, :with_subject => subject)
end
Clicking link:
When /^(?:I|they) click the first link in the email$/ do
click_first_link_in_email
end
I just looked at the confirmation email I got and my e-mail address is parsed as a mailto: link; I changed the first link step to this:
And they follow "Confirm my account" in the email
but that didn't work either.. I'm still getting the invalid token error message.

So.. I wasted two hours on this. I had a typo.
I have the following line:
unconfirmed = attributes.delete("unconfirmed") == "true"
I forgot to put the quotes around true. Without the quotes, the test passes..
Jeez.
Thanks to everyone who put some time in helping me :)

Related

AWS-SDK-SES: How Can I Check the Existence of an Email Address in Rails?

I currently have Rails applications that use SES to send emails. Unfortunately no matter how much code I put in my application I still get emails with invalid email addresses.
I want to use AWS to verify if I have a valid email address, meaning that the syntax is correct and does other verification like checking if the mailbox exists.
I installed the aws-sdk-rails gem in my application. I added my access_key_id and secret_access_key to config/credentials.yml.enc.
I added the following code from this awsdocs/aws-doc-sdk-examples GitHub repository to my contact form and made minor changes.
require 'aws-sdk-ses'
# Replace recipient#example.com with a "To" address.
recipient = params[:email]
error = " "
# Create a new SES resource in the us-west-2 region.
# Replace us-west-2 with the AWS Region you're using for Amazon SES.
ses = Aws::SES::Client.new(region: 'us-west-2')
# Try to verify email address.
begin
ses.verify_email_identity({
email_address: recipient
})
puts 'Email sent to ' + recipient
# If something goes wrong, display an error message.
rescue Aws::SES::Errors::ServiceError => error
puts "Email not sent. Error message: #{error}"
end
I entered an email address that AWS SES said that the mailbox didn't exist this morning. However when I ran this code it didn't produce an error as I thought it might. When I checked the Rails debug log error was blank. The region in my code is the one that I use to successfully send transactional emails from all my websites.
I couldn't find any documentation about that code that says how much verification it does for the email address.
Can I use this gem to find if email addresses exist or have other problems like SES checks when an email has a To: email address?

Mandrill "Send A Copy Of Every Email To This Address" : CC or BCC?

On the 'Sending Defaults' settings page of mandrill, there is a setting 'Send A Copy Of Every Email To This Address' field.
I would like to know 2 things: Is this sent using 'to', 'CC' or 'BCC', and if I send to X users, do I receive X emails or just 1?
I assume 'BCC' and '1' are the answers, but mandrill's test suite doesnt actually send emails, so this function isn't triggered and I cannot find any documentation on this.

Braintree Error In Production Mode

I have implemented Braintree subscription payment in rails app. Everything works fine in development, however when I switched to production (I have registered with Braintree and got a real account, and I change all the key in environment)
I have tried to submit an invalid card information to test the app, the page keeps showing error.
I look at the application logs and it said
NoMethodError (undefined method `customer' for #<Braintree::ErrorResult:0x007f6ed80f1d80>):
Here's my create method, I follow your tutorial and it works fine in development
def create
if current_user.braintree_id?
customer = Braintree::Customer.find(current_user.braintree_id)
else
result = Braintree::Customer.create(
email: current_user.company_email,
company: current_user.company_name,
payment_method_nonce: params[:payment_method_nonce]
)
customer = result.customer
current_user.update(braintree_id: customer.id)
end
result = Braintree::Subscription.create(
payment_method_token: customer.payment_methods.find{ |pm| pm.default? }.token,
plan_id: params[:plan_id]
)
if result.success?
result.subscription.transactions.each do |transaction|
current_user.transactions.create(braintree_transaction_id: transaction.id,
plan_name: params[:plan_name],
price: transaction.amount.to_f,
start_date: transaction.subscription_details.billing_period_start_date,
end_date: transaction.subscription_details.billing_period_end_date,
subscription_id: result.subscription.id
)
end
current_user.update(braintree_subscription_id: result.subscription.id,
next_billing_date: result.subscription.next_billing_date,
billing_period_start_date: result.subscription.billing_period_start_date,
billing_period_end_date: result.subscription.billing_period_end_date,
status: result.subscription.status,
next_billing_period_amount: result.subscription.next_billing_period_amount,
paid_through_date: result.subscription.paid_through_date,
plan_id: params[:plan_id],
plan_name: params[:plan_name])
flash[:info] = "You've been subscribed successfully"
redirect_to #current_user
else
flash[:warning] = "Invalid card information"
render 'new'
end
end
The weird thing is it doesn't render the flash warning of unsuccessful result and redirect to the original new_subscription_path, instead the website url redirect to this
https://herokuappname.herokuapp.com/subscription.1
and the page error shows
This page isn’t working herokuappname.herokuapp.com is currently unable to handle this request.
HTTP ERROR 500
So, I want to know whether it is the customer method error (which I don't think so because it doesn't have any problem in development mode) or any other problem such as why the page url so weird?
I looked at the Braintree control panel, and the reason that the subscription failed was because the bank declined the transactions due to incorrect card information, which I entered incorrect card in order to test it, if it is invalid card info, why didn't it display the flash notice and redirect back to the new_subscription_path, instead it redirects to the subscription.1 url which I have mentioned above?
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
I'm not sure if you used the same invalid card number for testing in production as in sandbox, but I'll try to answer your questions with the information we have on hand:
NoMethodError (undefined method `customer' for #):
By attempting to create a customer with a payment method with an invalid card, the result of that API call was an ErrorResult object. ErrorResult objects are either a validation error, processor decline, gateway rejection, or other exception messages, and do not contain a customer method. Hence, the undefined method error.
You should add some error handling around all of your Braintree API calls so that you can address any errors throughout the subscription process.

cucumber does not recognize my steps

Feature: Home page
As a HR manager of a company I am visiting the digipresent app for the first time
I want to be welcomed with a greeting
Scenario: User sees the welcome message
When: he goes to home page
Then: he should see the welcome message
This file is named as features/home_page.feature
When (/^he goes to the home page$/) do
visit root_path
end
Then (/^he should see the welcome message$/)do
expect(page).to have_content("Welcome to Present")
end
This file is present as features/step_definitions/home_page_steps.rb
$cucumber --require features features/home_page.feature when I do this, the steps are not getting recognised.
Feature: Home page
As a HR manager of a company I am visiting the digipresent app for the first time
I want to be welcomed with a greeting
Scenario: User sees the welcome message # features/home_page.feature:5
When: he goes to home page
Then: he should see the welcome message
1 scenario (1 passed)
0 steps
0m0.023s
There is no semicolon after when and then
Scenario: User sees the welcome message
When he goes to home page
Then he should see the welcome message
This should work

email confirmation message after auto login

I am using grails 2.3.2 and spring security rc2 for authenication and able to have auto login and send confirmation email via following :
sendMail {
from "somone#somthing.com.np"
to params.user.username
subject "Registration "
body "You have register as following ID :"+institution.id+" \n" +
"Please click following link for verification "+
" \n"+baseURL+"/home/compare?code="+random+""
}
Authentication authentication = new UsernamePasswordAuthenticationToken(params.user.username, params.user.password);
SecurityContextHolder.getContext().setAuthentication(authentication);
but i need message to display at each page if the user has not clicked the confirmation link and if the user has clicked the confirmation email then the message should not appear.Is there any approach to do this please help...
Maintain conformStatus in User domain. set nullable :true.set boolean
when that url activate by the user, set true confirmationStatus in User domain.
In gsp,
<g:if test="{session.loggedInUser.confirmStatus== false}">
display like click confirmation link from your mail
</g:if>

Resources