Skip to content

Commit

Permalink
Add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 14, 2024
1 parent d32d2f9 commit 3a762d1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::model::prelude::*;
mod cache_update;
mod event;
mod settings;
mod wrappers;
pub(crate) mod wrappers;

#[cfg(feature = "temp_cache")]
pub(crate) use wrappers::MaybeOwnedArc;
Expand Down
2 changes: 1 addition & 1 deletion src/cache/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use typesize::TypeSize;

#[derive(Debug)]
/// A wrapper around Option<DashMap<K, V>> to ease disabling specific cache fields.
pub(crate) struct MaybeMap<K: Eq + Hash, V>(pub(super) Option<DashMap<K, V, BuildHasher>>);
pub(crate) struct MaybeMap<K: Eq + Hash, V>(pub(crate) Option<DashMap<K, V, BuildHasher>>);
impl<K: Eq + Hash, V> MaybeMap<K, V> {
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V, BuildHasher>> {
Option::iter(&self.0).flat_map(DashMap::iter)
Expand Down
71 changes: 71 additions & 0 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,3 +1452,74 @@ pub struct PollAnswerCount {
pub count: u64,
pub me_voted: bool,
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use dashmap::DashMap;

use super::{
Guild,
GuildChannel,
Member,
Message,
PermissionOverwrite,
PermissionOverwriteType,
Permissions,
User,
UserId,
};
use crate::cache::wrappers::MaybeMap;
use crate::cache::Cache;

/// Test that author_permissions checks the permissions in a channel, not just the guild.
#[test]
fn author_permissions_respects_overwrites() {
// Author of the message, with a random ID that won't collide with defaults.
let author = User {
id: UserId::new(50778944701071),
..Default::default()
};

// Channel with the message, with SEND_MESSAGES on.
let channel = GuildChannel {
permission_overwrites: vec![PermissionOverwrite {
allow: Permissions::SEND_MESSAGES,
deny: Permissions::default(),
kind: PermissionOverwriteType::Member(author.id),
}],
..Default::default()
};
let channel_id = channel.id;

// Guild with the author and channel cached, default (empty) permissions.
let guild = Guild {
channels: HashMap::from([(channel.id, channel)]),
members: HashMap::from([(author.id, Member {
user: author.clone(),
..Default::default()
})]),
..Default::default()
};

// Message, tied to the guild and the channel.
let message = Message {
author,
channel_id,
guild_id: Some(guild.id),
..Default::default()
};

// Cache, with the guild setup.
let mut cache = Cache::new();
cache.guilds = MaybeMap(Some({
let guilds = DashMap::default();
guilds.insert(guild.id, guild);
guilds
}));

// The author should only have the one permission, SEND_MESSAGES.
assert_eq!(message.author_permissions(&cache), Some(Permissions::SEND_MESSAGES));
}
}

0 comments on commit 3a762d1

Please sign in to comment.