Skip to content

Commit

Permalink
ci: Add simple player files test to sanity test that upload works
Browse files Browse the repository at this point in the history
  • Loading branch information
kirre-bylund committed Nov 13, 2024
1 parent d4208ec commit 7d5d295
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
136 changes: 136 additions & 0 deletions Tests/LootLockerTests/PlayMode/PlayerFilesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

using LootLocker;
using LootLocker.Requests;
using LootLockerTestConfigurationUtils;
using NUnit.Framework;
using System;
using System.Collections;
using System.IO;
using System.Net;
using UnityEngine;
using UnityEngine.TestTools;

namespace LootLockerTests.PlayMode
{
public class PlayerFilesTest
{
private LootLockerTestGame gameUnderTest = null;
private LootLockerConfig configCopy = null;
private static int TestCounter = 0;
private bool SetupFailed = false;

[UnitySetUp]
public IEnumerator Setup()
{
TestCounter++;
configCopy = LootLockerConfig.current;

if (!LootLockerConfig.ClearSettings())
{
Debug.LogError("Could not clear LootLocker config");
}

// Create game
bool gameCreationCallCompleted = false;
LootLockerTestGame.CreateGame(testName: "GuestSessionTest" + TestCounter + " ", onComplete: (success, errorMessage, game) =>
{
if (!success)
{
gameCreationCallCompleted = true;
Debug.LogError(errorMessage);
SetupFailed = true;
}
gameUnderTest = game;
gameCreationCallCompleted = true;
});
yield return new WaitUntil(() => gameCreationCallCompleted);
if (SetupFailed)
{
yield break;
}
gameUnderTest?.SwitchToStageEnvironment();

// Enable guest platform
bool enableGuestLoginCallCompleted = false;
gameUnderTest?.EnableGuestLogin((success, errorMessage) =>
{
if (!success)
{
Debug.LogError(errorMessage);
SetupFailed = true;
}
enableGuestLoginCallCompleted = true;
});
yield return new WaitUntil(() => enableGuestLoginCallCompleted);
if (SetupFailed)
{
yield break;
}
Assert.IsTrue(gameUnderTest?.InitializeLootLockerSDK(), "Successfully created test game and initialized LootLocker");

// Sign in client
bool guestLoginCompleted = false;
LootLockerSDKManager.StartGuestSession(Guid.NewGuid().ToString(), response =>
{
SetupFailed |= !response.success;
guestLoginCompleted = true;
});
yield return new WaitUntil(() => guestLoginCompleted);

}

[UnityTearDown]
public IEnumerator TearDown()
{
if (gameUnderTest != null)
{
bool gameDeletionCallCompleted = false;
gameUnderTest.DeleteGame(((success, errorMessage) =>
{
if (!success)
{
Debug.LogError(errorMessage);
}
gameUnderTest = null;
gameDeletionCallCompleted = true;
}));
yield return new WaitUntil(() => gameDeletionCallCompleted);
}

LootLockerConfig.CreateNewSettings(configCopy.apiKey, configCopy.game_version, configCopy.domainKey,
configCopy.currentDebugLevel, configCopy.allowTokenRefresh);
}


[UnityTest]
public IEnumerator PlayerFiles_UploadSimplePublicFile_Succeeds()
{
Assert.IsFalse(SetupFailed, "Failed to setup game");
// Given
string path = Application.temporaryCachePath + "/PlayerFileCanBeCreatedWithPathUpdatedAndThenDeleted-creation.txt";
string content = "First added line";
TextWriter writer = new StreamWriter(path);
writer.WriteLine(content);
writer.Close();

// When
LootLockerPlayerFile actualResponse = new LootLockerPlayerFile();
bool setToPublic = true;
bool playerFileUploadCompleted = false;
LootLockerSDKManager.UploadPlayerFile(path, "test", setToPublic, fileResponse =>
{
actualResponse = fileResponse;
playerFileUploadCompleted = true;
});

// Wait for response
yield return new WaitUntil(() => playerFileUploadCompleted);

// Then
Assert.IsTrue(actualResponse.success, "File upload failed");
Assert.Greater(actualResponse.size, 0, "File Size was 0");
Assert.AreEqual(setToPublic, actualResponse.is_public, "File does not have the same public setting");
}
}
}
2 changes: 2 additions & 0 deletions Tests/LootLockerTests/PlayMode/PlayerFilesTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d5d295

Please sign in to comment.