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

Commit

Permalink
added unique index on room_id in rtcs
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Dec 15, 2020
1 parent 62437ee commit da24b7d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions migrations/2020-12-07-131734_unique_rtc_per_room/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS rtc_unique_room_id;
1 change: 1 addition & 0 deletions migrations/2020-12-07-131734_unique_rtc_per_room/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX rtc_unique_room_id ON rtc (room_id);
44 changes: 44 additions & 0 deletions src/app/endpoint/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,50 @@ mod test {
});
}

#[test]
fn create_rtc_duplicate() {
async_std::task::block_on(async {
let db = TestDb::new();
let mut authz = TestAuthz::new();

// Insert a room.
let room = db
.connection_pool()
.get()
.map(|conn| shared_helpers::insert_room(&conn))
.unwrap();

// Allow user to create rtcs in the room.
let agent = TestAgent::new("web", "user123", USR_AUDIENCE);
let room_id = room.id().to_string();
let object = vec!["rooms", &room_id, "rtcs"];
authz.allow(agent.account_id(), object, "create");

// Make rtc.create request.
let mut context = TestContext::new(db, authz);
let payload = CreateRequest { room_id: room.id() };

let messages = handle_request::<CreateHandler>(&mut context, &agent, payload)
.await
.expect("Rtc creation failed");

// Assert response.
let (rtc, respp) = find_response::<Rtc>(messages.as_slice());
assert_eq!(respp.status(), ResponseStatus::CREATED);
assert_eq!(rtc.room_id(), room.id());

// Make rtc.create request second time.
let payload = CreateRequest { room_id: room.id() };
let err = handle_request::<CreateHandler>(&mut context, &agent, payload)
.await
.expect_err("Unexpected success on rtc creation");

// This should fail with already exists
assert_eq!(err.status(), ResponseStatus::UNPROCESSABLE_ENTITY);
assert_eq!(err.kind(), "database_query_failed");
});
}

#[test]
fn create_rtc_unauthorized() {
async_std::task::block_on(async {
Expand Down

0 comments on commit da24b7d

Please sign in to comment.