Rails Integration test for edit/update - ruby-on-rails-4

I am trying run an integartion test, testing an email that is sent after the user uses the edit action of the order controller to assign the shipping date of the product.
This is what I have so far:
# the user visits the order edit page
get "orders/#{order.id}/edit"
assert_response :success
# the user submits the edit form with a shipped_on date
# a nonredirect response is returned
post_via_redirect "/orders/#{order.id}/edit",
order: {
ship_date: "2014-3-13 20:20:20"
}
assert_response :success
#check that the user was redirected to edit template
assert_template "edit"
I think this is not working because I am making a POST request and not at PUT request. So my question is, how do I make the PUT request to the update action and pass the new information that would normally be in the form submitted through the browser.

There is a method put_via_redirect , so it can be used in your test.
More information about the method is there http://apidock.com/rails/ActionController/Integration/Session/put_via_redirect

Related

Handling Django Registrations for Duplicate Entries

Question PART 1:
Project => App => forms.py
![User Creation Form using default Django Structure]
Project => App => urls.py
![URL Routes defined in urls.py of Django App]
Project => App => views.py
![User Authentication using default Django Structure]
Instead of using SQLite, I am using MongoDB Database and the entries are being stored in the auth_user table but I wanna get rid of duplicate entries (i.e. same username/email). If the user enters a same username/email, the page gives a DatabaseError Exception which I believe is handled by Django itself.
Question PART 2:
Project => settings.py
![Have included MessageTags into settings.py]
Also how to give specific redirect locations to messages in Django !? Once the user completes registration, the success message is displayed and he is redirected to the login page to access the dashboard and other features. But when the user logs in successfully, he is redirected to the dashboard but the Success Message is being displayed when he logs out (in the logout page).
Approach PART 1:
Can I, using try catch, handle the exception and allow the user to change the entered values to something unique and get his details saved into the database !?
Approach PART 2:
Sometimes the messages are appearing onto different pages than specified pages while using render/redirect for views.py functions(i.e routes). I tried redirecting each message to specific routes but I guess it overwrites to the last render/redirect route.
Regards,
Joe.

Is it even possible to post to a facebook page as a admin [duplicate]

I know how to make a post to a Facebook page via the API using PHP SDK, that is done like this:
$facebook->api('/xxxxxxxxxxx/feed', 'post', array('message'=> 'Hello world!', 'cb' => ''));
Where xxxxxxxxxxx is page id ;)
But doing that, I post to that page as me, Jamie, and not as the Page itself (admin).
So how do I post as Admin/Page instead of myself?
Thanks you for your time!
ANSWER (for lazy people):
First of all you need to make sure you have access to manage pages for user, ex:
<fb:login-button autologoutlink="true" perms="manage_pages"></fb:login-button>
Now you also gets a special token for every page user have access to once you get them.
PHP SDK Example:
//Get pages user id admin for
$fb_accounts = $facebook->api('/me/accounts');
//$fb_accounts is now an array
//holding all data on the pages user is admin for,
//we are interested in access_token
//We save the token for one of the pages into a variable
$access_token = $fb_accounts['data'][0]['access_token'];
//We can now update a Page as Admin
$facebook->api('/PAGE_ID/feed', 'post', array('message'=> 'Hello!', 'access_token' => $access_token, 'cb' => ''));
Check out this note regarding your question: http://developers.facebook.com/docs/api > Authorization > Page impersonation.
Long story short, you need the manage_pages extended permission.
And btw, have you checked this first ?

Integration Testing Django All-Auth Email Confirmation

I'd like to do some integration testing and I'm stuck on how I should test the email confirmation bit of the sign up process.
My user story is (basically) that a new visitor comes to the site, decides that they want to sign up, confirm their email address and then get redirected to their shiny new profile.
How would I simulate clicking on the link in the email to get redirected to the user profile?
I'm currently using Selenium to conduct my functional/integration testing and to a certain extent the django test suite.
These two documentation pages have everything you need:
https://docs.djangoproject.com/en/1.10/topics/testing/overview/
https://docs.djangoproject.com/en/1.10/topics/testing/tools/#email-services
A simple example:
from django.test import TestCase
from django.core import mail
class EmailTestCase(TestCase):
def setUp(self):
## Do something
pass
def test_email_content(self):
## Do something that triggers an email.
## Check the number of emails.
print(len(mail.outbox))
## Check the content of the first email.
first_email = mail.outbox[0]
if first_email:
print(first_email.body)
You should look up a python way to read emails.
Steps:
Find the email
Store the link
Start up your selenium test, and open up said link
Validate that you are on the page

Jsoup captcha login with cookie

I have been trying to develop a small application that gets captcha from my academics page, displays it to the user, the user enters captcha and the program should display details. However, I am stuck at login page itself. I used jsoup to get the cookie,save captcha to a folder, submit captcha along with form data, using the previous cookie. But I get the same login page again. Please help!
This is the get request. Website url https://academics.vit.ac.in/parent/parent_login.asp
Response res = Jsoup.connect(webSiteURL).method(Method.GET).execute();
Map<String,String> cook = res.cookies();
String sessionid = res.cookie("ASPSESSIONIDSQHDAQRQ");
After getting the captcha, this is the post request.
Response login = Jsoup.connect(webSiteURL)
.userAgent("Mozilla/5.0")
.cookie("ASPSESSIONIDSQHDAQRQ", sessionid).
data("message","Enter Verification Code of 6 characters exactly as shown.")
.data("vrfcd",captcha).data("wdpswd","somedata").data("wdregno","somedata")
.method(Method.POST).execute();
System.out.println(login.parse());
When I tried this
System.out.println(login.cookies());
The value is null, is that a clue?? Help me out!! Thanks!!
Are you sure you are retrieving the captcha using the cookie too?. Please check that. I mean, you can download the captcha with something like:
Response res1 = Jsoup.connect("https://academics.vit.ac.in/parent/captcha.asp")
.cookie("ASPSESSIONIDSQHDAQRQ", sessionid)
.ignoreContentType(true)
.method(Method.GET).timeout(30000).execute();
Then be sure that in the login post you are using the correct url, that is:
https://academics.vit.ac.in/parent/parent_login_submit.asp
Then it should work fine.
Hope it helps.

Catching failed OmniAuth login attempt information in Rails 4 application

Goal: I'm trying to get a Ruby on Rails application to send me emails whenever a user fails to log into OmniAuth. I want the e-mail to include (1) the username entered in the form, and (2) an MD5 hash of the password field.
Obstacle: OmniAuth returns a POST after a successful login, and a GET after an authentication failure. The "success" POST includes the username and a filtered password, but the "fail" GET does not include these two parameters.
So I guess my question is "Can I make OmniAuth return the parameters I want? If not, how can I make Rails remember the form data after it gets POST'ed to OmniAuth?"
I emailed the OmniAuth team and they gave me the solution below (thank you so much!):
You can do custom failure handling by adding an on_failure action.
OmniAuth.config.on_failure = Proc.new { |env| #do stuff }
https://github.com/intridea/omniauth/blob/master/lib/omniauth/failure_endpoint.rb
is the default failure endpoint as an example
So I added the following in config/initializers/omniauth.rb:
OmniAuth.config.on_failure = Proc.new{|env|
myLog = ActiveSupport::TaggedLogging.new(Logger.new("log/omniauth_log.txt"))
myLog.tagged("OmniAuth", "ENV") { myLog.info "Failed login attempt - username: #{env["rack.request.form_hash"]["username"]}, password: #{env["rack.request.form_hash"]["password"]} "}
OmniAuth::FailureEndpoint.new(env).redirect_to_failure}
...and it records the username and password correctly. All that's left to do is encrypt the password.
If you want to display everything that's going on, you can log #{env.inspect} itself. It's a very large hash though (that also contains smaller hashes), so maybe log #{env.inspect} once and pick out the fields relevant to your task.