Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Put agent in progress on double room.enter (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Oct 17, 2019
1 parent ed072e3 commit c12c9dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
53 changes: 53 additions & 0 deletions src/app/endpoint/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,59 @@ mod test {
});
}

#[test]
fn enter_room_twice() {
use crate::db::agent::{Object, Status};
use crate::schema::agent as agent_schema;

futures::executor::block_on(async {
let db = TestDb::new();
let mut authz = TestAuthz::new(AUDIENCE);
let agent = TestAgent::new("web", "user123", AUDIENCE);

// Insert a room and an agent in `ready` status.
let room = db
.connection_pool()
.get()
.map_err(|err| format_err!("Failed to get DB connection: {}", err))
.and_then(|conn| {
let room = insert_room(&conn, AUDIENCE);

factory::Agent::new()
.audience(AUDIENCE)
.room_id(room.id())
.agent_id(agent.agent_id())
.status(Status::Ready)
.insert(&conn)?;

Ok(room)
})
.unwrap();

// Allow the agent to enter the room.
let room_id = room.id().to_string();
let object = vec!["rooms", &room_id, "events"];
authz.allow(agent.account_id(), object, "subscribe");

// Make room.enter request.
let payload = json!({"id": room.id()});
let state = State::new(authz.into(), db.connection_pool().clone());
let request: EnterRequest = agent.build_request("room.enter", &payload).unwrap();
state.enter(request).await.into_result().unwrap();

// Assert agent is in `in_progress` state in the DB.
let conn = db.connection_pool().get().unwrap();

let agent_object = agent_schema::table
.filter(agent_schema::agent_id.eq(agent.agent_id()))
.filter(agent_schema::room_id.eq(room.id()))
.get_result::<Object>(&conn)
.expect("Agent not found in the DB");

assert_eq!(*agent_object.status(), Status::InProgress);
});
}

///////////////////////////////////////////////////////////////////////////

#[test]
Expand Down
5 changes: 3 additions & 2 deletions src/db/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,13 @@ impl<'a> InsertQuery<'a> {

pub(crate) fn execute(&self, conn: &PgConnection) -> Result<Object, Error> {
use crate::schema::agent::dsl::*;
use diesel::RunQueryDsl;
use diesel::{ExpressionMethods, RunQueryDsl};

diesel::insert_into(agent)
.values(self)
.on_conflict((agent_id, room_id))
.do_nothing()
.do_update()
.set(status.eq(Status::InProgress))
.get_result(conn)
}
}
Expand Down

0 comments on commit c12c9dc

Please sign in to comment.