Skip to content

Commit

Permalink
Implement support for nonce enforcement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Apr 29, 2024
1 parent cccd5bd commit f5426da
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ Task<Result<IMessage>> GetChannelMessageAsync
/// mentioned in this parameter will be deleted.
/// </param>
/// <param name="flags">The message flags.</param>
/// <param name="enforceNonce">Indicates whether messages should be deduplicated using the passed nonce.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A creation result which may or may not have succeeded.</returns>
Task<Result<IMessage>> CreateMessageAsync
Expand All @@ -507,6 +508,7 @@ Task<Result<IMessage>> CreateMessageAsync
Optional<IReadOnlyList<Snowflake>> stickerIDs = default,
Optional<IReadOnlyList<OneOf<FileData, IPartialAttachment>>> attachments = default,
Optional<MessageFlags> flags = default,
Optional<bool> enforceNonce = default,
CancellationToken ct = default
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public async Task<Result<IMessage>> CreateMessageAsync
Optional<IReadOnlyList<Snowflake>> stickerIds = default,
Optional<IReadOnlyList<OneOf<FileData, IPartialAttachment>>> attachments = default,
Optional<MessageFlags> flags = default,
Optional<bool> enforceNonce = default,
CancellationToken ct = default
)
{
Expand All @@ -243,6 +244,7 @@ public async Task<Result<IMessage>> CreateMessageAsync
stickerIds,
attachments,
flags,
enforceNonce,
ct
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ public virtual async Task<Result<IMessage>> CreateMessageAsync
Optional<IReadOnlyList<Snowflake>> stickerIDs = default,
Optional<IReadOnlyList<OneOf<FileData, IPartialAttachment>>> attachments = default,
Optional<MessageFlags> flags = default,
Optional<bool> enforceNonce = default,
CancellationToken ct = default
)
{
Expand Down Expand Up @@ -649,6 +650,7 @@ public virtual async Task<Result<IMessage>> CreateMessageAsync
json.Write("sticker_ids", stickerIDs, this.JsonOptions);
json.Write("attachments", attachmentList, this.JsonOptions);
json.Write("flags", flags, this.JsonOptions);
json.Write("enforce_nonce", enforceNonce, this.JsonOptions);
}
)
.WithRateLimitContext(this.RateLimitCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ public async Task PerformsNormalRequestCorrectly()
var tts = false;
var allowedMentions = new AllowedMentions();
var flags = MessageFlags.SuppressEmbeds;
var enforceNonce = true;

var api = CreateAPI
(
Expand All @@ -1122,6 +1123,7 @@ public async Task PerformsNormalRequestCorrectly()
.WithProperty("tts", p => p.Is(tts))
.WithProperty("allowed_mentions", p => p.IsObject())
.WithProperty("flags", p => p.Is((int)flags))
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1134,7 +1136,8 @@ public async Task PerformsNormalRequestCorrectly()
nonce,
tts,
allowedMentions: allowedMentions,
flags: flags
flags: flags,
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand All @@ -1153,6 +1156,7 @@ public async Task PerformsEmbedRequestCorrectly()
var nonce = "aasda";
var tts = false;
var allowedMentions = new AllowedMentions();
var enforceNonce = true;

var api = CreateAPI
(
Expand All @@ -1167,6 +1171,7 @@ public async Task PerformsEmbedRequestCorrectly()
.WithProperty("nonce", p => p.Is(nonce))
.WithProperty("tts", p => p.Is(tts))
.WithProperty("allowed_mentions", p => p.IsObject())
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1178,7 +1183,8 @@ public async Task PerformsEmbedRequestCorrectly()
nonce: nonce,
isTTS: tts,
embeds: embeds,
allowedMentions: allowedMentions
allowedMentions: allowedMentions,
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand All @@ -1198,6 +1204,7 @@ public async Task PerformsComponentRequestCorrectly()
var tts = false;
var allowedMentions = new AllowedMentions();
var components = new List<IMessageComponent>();
var enforceNonce = true;

var api = CreateAPI
(
Expand All @@ -1213,6 +1220,7 @@ public async Task PerformsComponentRequestCorrectly()
.WithProperty("tts", p => p.Is(tts))
.WithProperty("allowed_mentions", p => p.IsObject())
.WithProperty("components", p => p.IsArray())
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1225,7 +1233,8 @@ public async Task PerformsComponentRequestCorrectly()
isTTS: tts,
embeds: embeds,
allowedMentions: allowedMentions,
components: components
components: components,
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand All @@ -1247,6 +1256,7 @@ public async Task PerformsFileUploadRequestCorrectly()

var nonce = "aasda";
var tts = false;
var enforceNonce = true;

var api = CreateAPI
(
Expand Down Expand Up @@ -1279,6 +1289,7 @@ public async Task PerformsFileUploadRequestCorrectly()
)
)
)
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1289,7 +1300,8 @@ public async Task PerformsFileUploadRequestCorrectly()
channelId,
nonce: nonce,
isTTS: tts,
attachments: new OneOf<FileData, IPartialAttachment>[] { new FileData(fileName, file, description) }
attachments: new OneOf<FileData, IPartialAttachment>[] { new FileData(fileName, file, description) },
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand All @@ -1315,6 +1327,7 @@ public async Task PerformsMultiFileUploadRequestCorrectly()

var nonce = "aasda";
var tts = false;
var enforceNonce = true;

var api = CreateAPI
(
Expand Down Expand Up @@ -1359,6 +1372,7 @@ public async Task PerformsMultiFileUploadRequestCorrectly()
)
)
)
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1373,7 +1387,8 @@ public async Task PerformsMultiFileUploadRequestCorrectly()
{
new FileData(fileName1, file1, description1),
new FileData(fileName2, file2, description2)
}
},
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand All @@ -1396,6 +1411,7 @@ public async Task PerformsRetainingFileUploadRequestCorrectly()

var nonce = "aasda";
var tts = false;
var enforceNonce = true;

var api = CreateAPI
(
Expand Down Expand Up @@ -1437,6 +1453,7 @@ public async Task PerformsRetainingFileUploadRequestCorrectly()
)
)
)
.WithProperty("enforce_nonce", p => p.Is(enforceNonce))
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -1451,7 +1468,8 @@ public async Task PerformsRetainingFileUploadRequestCorrectly()
{
new FileData(fileName, file, description),
new PartialAttachment(DiscordSnowflake.New(999))
}
},
enforceNonce: enforceNonce
);

ResultAssert.Successful(result);
Expand Down

0 comments on commit f5426da

Please sign in to comment.