Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test basic auth in my rspec controller examples?

I have some rails controllers that use authenticate_or_request_with_http_basic -- how can I set the user/pass in my rspec examples?

I'm surprised to not have been able to find this elsewhere on the web, so maybe I'm using the wrong terms? -- I found some very old things, or what seems to me like very complicated things. Seems like at this point rspec would either have direct support for this, or a tidy idiomatic solution.

like image 315
John Bachir Avatar asked Feb 25 '13 21:02

John Bachir


People also ask

How do you test basic authentication?

Testing Basic Auth with httpbin The endpoint for Basic Auth is /basic-auth/{user}/{passwd} . For example, if you go to http://httpbin.org/basic-auth/foo/bar you'll see a prompt and you can authenticate using the username foo and the password bar .

What is RSpec testing?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What does RSpec describe do?

The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.


1 Answers

this works. thanks to @zetetic pointing me in the right direction

before do
  @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
end
like image 189
John Bachir Avatar answered Oct 19 '22 05:10

John Bachir