Functional testing and HTTP authentication

Hi,

If I want my functional tests to bypass the built in Rails HTTP authentication, how would I do that?

Would I use something like FlexMock to stub out the :before_filter for my controller? Could someone please provide an example, because I'm not really understanding the basics for this scenario.

Cheers, Peter

I'm assuming that the lack of replies suggests that this is the wrong way to go about. Should I provide some login details in my tests or what would you suggest?

// Peter

Peter Hultgren wrote in post #949752:

Hi,

If I want my functional tests

(which you should replace with Cucumber scenarios)

to bypass the built in Rails HTTP authentication, how would I do that?

Why do you want to do that? What are you trying to achieve?

Would I use something like FlexMock to stub out the :before_filter for my controller? Could someone please provide an example, because I'm not really understanding the basics for this scenario.

It's hard to understand your use case, and without your ultimate goal, it's very difficult to give useful suggestions.

Cheers, Peter

Best,

Ah, so the question was unclear. Let me try to rephrase it:

I want to write a test against my controller to make sure that the actions respond as they should, but since I have HTTP authentication turned on I get failures like this: "response to be a <:success>, but was <401>"

So the test runs just fine with authentication turned off, but fails when turned on. What should I do to get past this and be able to test the logic of my controller?

// Peter

Please quote when replying.

Peter Hultgren wrote in post #949955:

Ah, so the question was unclear. Let me try to rephrase it:

I want to write a test against my controller to make sure that the actions respond as they should, but since I have HTTP authentication turned on I get failures like this: "response to be a <:success>, but was <401>"

So the test runs just fine with authentication turned off, but fails when turned on. What should I do to get past this and be able to test the logic of my controller?

Have your test code log in at the beginning of your tests. With controller tests, that probably goes in a before block; with Cucumber stories (which I highly recommend using instead), you actually want that in your scenarios themselves (or your scenario background), in order to better document and model the workflow.

// Peter

Best,

Marnen Laibow-Koser wrote in post #949979:

Please quote when replying.

Peter Hultgren wrote in post #949955:

Ah, so the question was unclear. Let me try to rephrase it:

I want to write a test against my controller to make sure that the actions respond as they should, but since I have HTTP authentication turned on I get failures like this: "response to be a <:success>, but was <401>"

So the test runs just fine with authentication turned off, but fails when turned on. What should I do to get past this and be able to test the logic of my controller?

Have your test code log in at the beginning of your tests. With controller tests, that probably goes in a before block;

On second thought, in this case, you might want to stub your before_filter. Or you might not. The rest of my advice stands.

Peter Hultgren wrote in post #949955:

Ah, so the question was unclear. Let me try to rephrase it:

I want to write a test against my controller to make sure that the actions respond as they should, but since I have HTTP authentication turned on I get failures like this: "response to be a <:success>, but was <401>"

So the test runs just fine with authentication turned off, but fails when turned on. What should I do to get past this and be able to test the logic of my controller?

With RSpec and FactoryGirl I do something like:

describe "authenticated" do   before(:each) do     login_as(Factory(:user))   end

  it "does something as a logged in user" do     ...   end end

/lib/AuthenticatedTestHelper

Robert Walker wrote in post #950276:

Peter Hultgren wrote in post #949955:

Ah, so the question was unclear. Let me try to rephrase it:

I want to write a test against my controller to make sure that the actions respond as they should, but since I have HTTP authentication turned on I get failures like this: "response to be a <:success>, but was <401>"

So the test runs just fine with authentication turned off, but fails when turned on. What should I do to get past this and be able to test the logic of my controller?

With RSpec and FactoryGirl I do something like:

describe "authenticated" do   before(:each) do     login_as(Factory(:user))   end

  it "does something as a logged in user" do     ...   end end

That works, to be cure, but why are you using RSpec on your controllers? That's a job for Cucumber.

Best,

Marnen Laibow-Koser wrote in post #950279:

That works, to be cure, but why are you using RSpec on your controllers? That's a job for Cucumber.

Mostly because I pulled this from an older project where I wasn't yet using Cucumber. I was just learning RSpec at the time. I might do things differently today.