1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 18:37:41 +02:00

fix doctest ci (#188)

This commit is contained in:
Rob Ede
2021-06-27 07:02:38 +01:00
committed by GitHub
parent 64eec6e550
commit 20ef05c36e
23 changed files with 255 additions and 206 deletions

View File

@ -4,11 +4,9 @@ version = "0.10.0-beta.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Redis integration for Actix and session store for Actix Web"
license = "MIT OR Apache-2.0"
readme = "README.md"
keywords = ["actix", "redis", "async", "session"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-extras.git"
documentation = "https://docs.rs/actix-redis/"
repository = "https://github.com/actix/actix-extras"
categories = ["network-programming", "asynchronous"]
exclude = [".cargo/config"]
edition = "2018"
@ -31,9 +29,9 @@ web = [
]
[dependencies]
actix = { version = "0.11.0", default-features = false }
actix = { version = "0.12.0", default-features = false }
actix-rt = { version = "2.1", default-features = false }
actix-service = "2.0.0-beta.5"
actix-service = "2.0.0"
actix-tls = { version = "3.0.0-beta.5", default-features = false, features = ["connect"] }
log = "0.4.6"
@ -47,15 +45,23 @@ tokio = { version = "1", features = ["sync"] }
tokio-util = "0.6.1"
# actix-session
actix-web = { version = "4.0.0-beta.5", default_features = false, optional = true }
actix-web = { version = "4.0.0-beta.8", default_features = false, optional = true }
actix-session = { version = "0.5.0-beta.1", optional = true }
rand = { version = "0.8.0", optional = true }
serde = { version = "1.0.101", optional = true }
serde_json = { version = "1.0.40", optional = true }
[dev-dependencies]
actix-test = "0.1.0-beta.1"
actix-test = "0.1.0-beta.3"
actix-http = "3.0.0-beta.5"
actix-rt = "2.1"
env_logger = "0.7"
serde_derive = "1.0"
env_logger = "0.8"
serde = { version = "1.0.101", features = ["derive"] }
[[example]]
name = "basic"
required-features = ["web"]
[[example]]
name = "authentication"
required-features = ["web"]

View File

@ -1,7 +1,8 @@
use actix_redis::RedisSession;
use actix_session::Session;
use actix_web::{
cookie, middleware, web, App, Error, HttpResponse, HttpServer, Responder,
cookie, error::InternalError, middleware, web, App, Error, HttpResponse, HttpServer,
Responder,
};
use serde::{Deserialize, Serialize};
@ -49,12 +50,12 @@ pub fn validate_session(session: &Session) -> Result<i64, HttpResponse> {
async fn login(
credentials: web::Json<Credentials>,
session: Session,
) -> Result<impl Responder, HttpResponse> {
) -> Result<impl Responder, Error> {
let credentials = credentials.into_inner();
match User::authenticate(credentials) {
Ok(user) => session.insert("user_id", user.id).unwrap(),
Err(_) => return Err(HttpResponse::Unauthorized().json("Unauthorized")),
Err(err) => return Err(InternalError::from_response("", err).into()),
};
Ok("Welcome!")
@ -63,7 +64,7 @@ async fn login(
/// some protected resource
async fn secret(session: Session) -> Result<impl Responder, Error> {
// only allow access to this resource if the user has an active session
validate_session(&session)?;
validate_session(&session).map_err(|err| InternalError::from_response("", err))?;
Ok("secret revealed")
}

View File

@ -3,14 +3,16 @@ use std::{collections::HashMap, iter, rc::Rc};
use actix::prelude::*;
use actix_service::{Service, Transform};
use actix_session::{Session, SessionStatus};
use actix_web::cookie::{Cookie, CookieJar, Key, SameSite};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::http::header::{self, HeaderValue};
use actix_web::{error, Error, HttpMessage};
use actix_web::{
cookie::{Cookie, CookieJar, Key, SameSite},
dev::{ServiceRequest, ServiceResponse},
error,
http::header::{self, HeaderValue},
Error,
};
use futures_core::future::LocalBoxFuture;
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng};
use redis_async::resp::RespValue;
use redis_async::resp_array;
use redis_async::{resp::RespValue, resp_array};
use time::{self, Duration, OffsetDateTime};
use crate::redis::{Command, RedisActor};
@ -311,7 +313,7 @@ impl Inner {
// set cookie
let mut jar = CookieJar::new();
jar.signed(&self.key).add(cookie);
jar.signed_mut(&self.key).add(cookie);
(value, Some(jar))
};
@ -321,7 +323,7 @@ impl Inner {
let state: HashMap<_, _> = state.collect();
let body = match serde_json::to_string(&state) {
Err(e) => return Err(e.into()),
Err(err) => return Err(err.into()),
Ok(body) => body,
};
@ -442,12 +444,15 @@ mod test {
async fn logout(session: Session) -> Result<HttpResponse> {
let id: Option<String> = session.get("user_id")?;
if let Some(x) = id {
let body = if let Some(x) = id {
session.purge();
Ok(format!("Logged out: {}", x).into())
format!("Logged out: {}", x)
} else {
Ok("Could not log out anonymous user".into())
}
"Could not log out anonymous user".to_owned()
};
Ok(HttpResponse::Ok().body(body))
}
#[actix_rt::test]
@ -648,7 +653,10 @@ mod test {
.unwrap();
assert_ne!(
OffsetDateTime::now_utc().year(),
cookie_4.expires().map(|t| t.year()).unwrap()
cookie_4
.expires()
.map(|t| t.datetime().expect("Expiration is a datetime").year())
.unwrap()
);
// Step 10: GET index, including session cookie #2 in request