Skip to content

Begin working on a revamped test harness #1697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ensure that we never send both a cookie and a token for auth
  • Loading branch information
sgrif committed Apr 2, 2019
commit 2e3ee040e60884df6802c941b8a912feb48a03f9
14 changes: 11 additions & 3 deletions src/tests/helpers/request.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::response::{Response, ResponseError};
use cargo_registry::middleware::current_user::AuthenticationSource;
use cargo_registry::models::{ApiToken, User};
use conduit::{Handler, Method, Request};
use conduit_middleware::MiddlewareBuilder;
use conduit_test::MockRequest;
use super::response::{Response, ResponseError};

pub struct RequestBuilder<'a> {
middleware: &'a MiddlewareBuilder,
Expand All @@ -20,7 +21,7 @@ impl<'a> RequestBuilder<'a> {

/// Sends the request signed in as the given user
pub fn signed_in_as(mut self, user: &User) -> Self {
use cargo_registry::middleware::current_user::AuthenticationSource;
self.clear_auth();
self.request.mut_extensions().insert(user.clone());
self.request
.mut_extensions()
Expand All @@ -29,7 +30,8 @@ impl<'a> RequestBuilder<'a> {
}

/// Uses the given token for authentication
pub fn with_token(self, token: &ApiToken) -> Self {
pub fn with_token(mut self, token: &ApiToken) -> Self {
self.clear_auth();
self.with_header("Authorization", &token.token)
}

Expand All @@ -50,4 +52,10 @@ impl<'a> RequestBuilder<'a> {
pub fn send(mut self) -> Result<Response, ResponseError> {
Response::new(self.middleware.call(&mut self.request)?)
}

fn clear_auth(&mut self) {
self.request.mut_extensions().remove::<User>();
self.request.mut_extensions().remove::<AuthenticationSource>();
self.request.header("Authorization", "");
}
}