Fix MongoStorage nodeFromDoc integer cast (#3846)

This commit is contained in:
Luck 2024-08-24 09:55:32 +01:00
parent 9bf30fc839
commit 9d80de5d5c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 75 additions and 3 deletions

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.common.storage.implementation.mongodb; package me.lucko.luckperms.common.storage.implementation.mongodb;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions; import com.mongodb.MongoClientOptions;
@ -586,7 +587,8 @@ public class MongoStorage implements StorageImplementation {
return new Document("_id", track.getName()).append("groups", track.getGroups()); 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() Document document = new Document()
.append("key", node.getKey()) .append("key", node.getKey())
.append("value", node.getValue()); .append("value", node.getValue());
@ -603,7 +605,8 @@ public class MongoStorage implements StorageImplementation {
return document; 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"); String key = document.containsKey("permission") ? document.getString("permission") : document.getString("key");
if (key == null || key.isEmpty()) { if (key == null || key.isEmpty()) {
@ -622,7 +625,7 @@ public class MongoStorage implements StorageImplementation {
} }
if (document.containsKey("expiry")) { 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) { if (document.containsKey("context") && document.get("context") instanceof List) {

View File

@ -0,0 +1,69 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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());
}
}