From 9d80de5d5cbc15c75e01e4d1faf143fb9906ab42 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 24 Aug 2024 09:55:32 +0100 Subject: [PATCH] Fix MongoStorage nodeFromDoc integer cast (#3846) --- .../implementation/mongodb/MongoStorage.java | 9 ++- .../mongodb/MongoStorageUnitTest.java | 69 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 common/src/test/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorageUnitTest.java diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java index 922e096bb..82b44baf8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.storage.implementation.mongodb; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; @@ -586,7 +587,8 @@ public class MongoStorage implements StorageImplementation { return new Document("_id", track.getName()).append("groups", track.getGroups()); } - private static Document nodeToDoc(Node node) { + @VisibleForTesting + static Document nodeToDoc(Node node) { Document document = new Document() .append("key", node.getKey()) .append("value", node.getValue()); @@ -603,7 +605,8 @@ public class MongoStorage implements StorageImplementation { return document; } - private static Node nodeFromDoc(Document document) { + @VisibleForTesting + static Node nodeFromDoc(Document document) { String key = document.containsKey("permission") ? document.getString("permission") : document.getString("key"); if (key == null || key.isEmpty()) { @@ -622,7 +625,7 @@ public class MongoStorage implements StorageImplementation { } if (document.containsKey("expiry")) { - builder.expiry((long) document.get("expiry")); + builder.expiry(((Number) document.get("expiry")).longValue()); } if (document.containsKey("context") && document.get("context") instanceof List) { diff --git a/common/src/test/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorageUnitTest.java b/common/src/test/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorageUnitTest.java new file mode 100644 index 000000000..08c8451c0 --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorageUnitTest.java @@ -0,0 +1,69 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.storage.implementation.mongodb; + +import net.luckperms.api.node.Node; +import org.bson.Document; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class MongoStorageUnitTest { + + @Test + public void testNodeFromDocExpiryLong() { + Document document = new Document() + .append("key", "test") + .append("value", true) + .append("expiry", 1000L); + + Node node = MongoStorage.nodeFromDoc(document); + assertNotNull(node); + assertEquals("test", node.getKey()); + assertTrue(node.getValue()); + assertNotNull(node.getExpiry()); + assertEquals(1000, node.getExpiry().getEpochSecond()); + } + + // https://github.com/LuckPerms/LuckPerms/issues/3846 + @Test + public void testNodeFromDocExpiryInteger() { + Document document = new Document() + .append("key", "test") + .append("value", true) + .append("expiry", 1000); + + Node node = MongoStorage.nodeFromDoc(document); + assertNotNull(node); + assertEquals("test", node.getKey()); + assertTrue(node.getValue()); + assertNotNull(node.getExpiry()); + assertEquals(1000, node.getExpiry().getEpochSecond()); + } + +}