test: Add snapshot tests for imageuploader import/export (#6394)

This commit is contained in:
nerix
2025-08-14 18:07:50 +02:00
committed by GitHub
parent f3b02f370b
commit 8c05f37af3
14 changed files with 341 additions and 1 deletions
@@ -0,0 +1,22 @@
{
"input": {
"deletionLink": "{more}",
"formField": "form",
"headers": "My-Header: Foo",
"link": "foo{bar}baz",
"url": "http://example.com"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "{more}",
"FileFormName": "form",
"Headers": {
"My-Header": "Foo"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "http://example.com",
"URL": "foo{bar}baz",
"Version": "1.0.0"
}
}
@@ -0,0 +1,23 @@
{
"input": {
"deletionLink": "{more}",
"formField": "file",
"headers": "My-Header: Foo ; Bar : Baz ; KeyOnly\nX-My-Header:1",
"link": "foo{bar}baz",
"url": "https://example.com"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "{more}",
"FileFormName": "file",
"Headers": {
"My-Header": "Foo ; Bar : Baz ; KeyOnly",
"X-My-Header": "1"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://example.com",
"URL": "foo{bar}baz",
"Version": "1.0.0"
}
}
@@ -0,0 +1,22 @@
{
"input": {
"deletionLink": "",
"formField": "file",
"headers": "Authorization: Basic XXXXXXXXXXXXXXX",
"link": "",
"url": "https://i.yourwebsite.com/upload"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "",
"FileFormName": "file",
"Headers": {
"Authorization": "Basic XXXXXXXXXXXXXXX"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://i.yourwebsite.com/upload",
"URL": "",
"Version": "1.0.0"
}
}
@@ -0,0 +1,19 @@
{
"input": {
"deletionLink": "",
"formField": "attachment",
"headers": "",
"link": "",
"url": "https://i.nuuls.com/upload"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "",
"FileFormName": "attachment",
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://i.nuuls.com/upload",
"URL": "",
"Version": "1.0.0"
}
}
@@ -0,0 +1,22 @@
{
"input": {
"deletionLink": "https://imgur.com/delete/{data.deletehash}",
"formField": "image",
"headers": "Authorization: Client-ID c898c0bb848ca39",
"link": "{data.link}",
"url": "https://api.imgur.com/3/image"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "https://imgur.com/delete/{data.deletehash}",
"FileFormName": "image",
"Headers": {
"Authorization": "Client-ID c898c0bb848ca39"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://api.imgur.com/3/image",
"URL": "{data.link}",
"Version": "1.0.0"
}
}
@@ -0,0 +1,19 @@
{
"input": {
"deletionLink": "https://s-ul.eu/delete.php?file={filename}&key=XXXXXXXXXXXXXXX",
"formField": "file",
"headers": "",
"link": "{url}",
"url": "https://s-ul.eu/api/v1/upload?wizard=true&key=XXXXXXXXXXXXXXX"
},
"output": {
"Body": "MultipartFormData",
"DeletionURL": "https://s-ul.eu/delete.php?file={filename}&key=XXXXXXXXXXXXXXX",
"FileFormName": "file",
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://s-ul.eu/api/v1/upload?wizard=true&key=XXXXXXXXXXXXXXX",
"URL": "{url}",
"Version": "1.0.0"
}
}
@@ -0,0 +1,22 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "{more}",
"FileFormName": "form",
"Headers": {
"My-Header": "Foo"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "http://example.com",
"URL": "foo{bar}baz",
"Version": "1.0.0"
},
"output": {
"deletionLink": "{more}",
"formField": "form",
"headers": "My-Header: Foo",
"link": "foo{bar}baz",
"url": "http://example.com"
}
}
@@ -0,0 +1,24 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "{more}",
"FileFormName": "file",
"Headers": {
"Another": "header",
"My-Header": "Foo ; Bar : Baz ; KeyOnly",
"X-My-Header": "1"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://example.com",
"URL": "foo{bar}baz",
"Version": "1.0.0"
},
"output": {
"deletionLink": "{more}",
"formField": "file",
"headers": "Another: header\nMy-Header: Foo ; Bar : Baz ; KeyOnly\nX-My-Header: 1",
"link": "foo{bar}baz",
"url": "https://example.com"
}
}
@@ -0,0 +1,22 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "",
"FileFormName": "file",
"Headers": {
"Authorization": "Basic XXXXXXXXXXXXXXX"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://i.yourwebsite.com/upload",
"URL": "",
"Version": "1.0.0"
},
"output": {
"deletionLink": "",
"formField": "file",
"headers": "Authorization: Basic XXXXXXXXXXXXXXX",
"link": "",
"url": "https://i.yourwebsite.com/upload"
}
}
@@ -0,0 +1,19 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "",
"FileFormName": "attachment",
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://i.nuuls.com/upload",
"URL": "",
"Version": "1.0.0"
},
"output": {
"deletionLink": "",
"formField": "attachment",
"headers": "",
"link": "",
"url": "https://i.nuuls.com/upload"
}
}
@@ -0,0 +1,22 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "https://imgur.com/delete/{data.deletehash}",
"FileFormName": "image",
"Headers": {
"Authorization": "Client-ID c898c0bb848ca39"
},
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://api.imgur.com/3/image",
"URL": "{data.link}",
"Version": "1.0.0"
},
"output": {
"deletionLink": "https://imgur.com/delete/{data.deletehash}",
"formField": "image",
"headers": "Authorization: Client-ID c898c0bb848ca39",
"link": "{data.link}",
"url": "https://api.imgur.com/3/image"
}
}
@@ -0,0 +1,19 @@
{
"input": {
"Body": "MultipartFormData",
"DeletionURL": "https://s-ul.eu/delete.php?file={filename}&key=XXXXXXXXXXXXXXX",
"FileFormName": "file",
"Name": "Chatterino Image Uploader Settings",
"RequestMethod": "POST",
"RequestURL": "https://s-ul.eu/api/v1/upload?wizard=true&key=XXXXXXXXXXXXXXX",
"URL": "{url}",
"Version": "1.0.0"
},
"output": {
"deletionLink": "https://s-ul.eu/delete.php?file={filename}&key=XXXXXXXXXXXXXXX",
"formField": "file",
"headers": "",
"link": "{url}",
"url": "https://s-ul.eu/api/v1/upload?wizard=true&key=XXXXXXXXXXXXXXX"
}
}
+85
View File
@@ -1,7 +1,10 @@
#include "singletons/ImageUploader.hpp"
#include "common/network/NetworkResult.hpp"
#include "lib/Snapshot.hpp"
#include "mocks/BaseApplication.hpp"
#include "Test.hpp"
#include "util/ImageUploader.hpp"
#include <QJsonArray>
#include <QJsonDocument>
@@ -12,6 +15,12 @@
#include <utility>
#include <vector>
namespace {
constexpr bool UPDATE_SNAPSHOTS = false;
} // namespace
namespace chatterino::imageuploader::detail {
TEST(ImageUploaderDetail_GetJsonValue, object)
@@ -258,4 +267,80 @@ TEST(ImageUploaderDetail_GetLinkFromResponse, scalar)
ASSERT_EQ(getLinkFromResponse(res("null"), "-{a.b}-"), "--");
}
class ImageUploaderTest : public ::testing::Test
{
protected:
void configure()
{
this->app = std::make_unique<mock::BaseApplication>();
}
void TearDown() override
{
this->app.reset();
}
std::unique_ptr<mock::BaseApplication> app;
};
class ImportTest : public ImageUploaderTest,
public ::testing::WithParamInterface<QString>
{
};
TEST_P(ImportTest, Run)
{
auto fixture = testlib::Snapshot::read("ImageUploader/Import", GetParam());
configure();
detail::importSettings(fixture->input().toObject(), *getSettings());
ASSERT_TRUE(fixture->run(
QJsonObject{
{"url", getSettings()->imageUploaderUrl.getValue()},
{"formField", getSettings()->imageUploaderFormField.getValue()},
{"headers", getSettings()->imageUploaderHeaders.getValue()},
{"link", getSettings()->imageUploaderLink.getValue()},
{"deletionLink",
getSettings()->imageUploaderDeletionLink.getValue()},
},
UPDATE_SNAPSHOTS));
}
INSTANTIATE_TEST_SUITE_P(
ImageUploader, ImportTest,
testing::ValuesIn(testlib::Snapshot::discover("ImageUploader/Import")));
class ExportTest : public ImageUploaderTest,
public ::testing::WithParamInterface<QString>
{
};
TEST_P(ExportTest, Run)
{
auto fixture = testlib::Snapshot::read("ImageUploader/Export", GetParam());
configure();
const auto input = fixture->input().toObject();
getSettings()->imageUploaderUrl = input["url"].toString();
getSettings()->imageUploaderFormField = input["formField"].toString();
getSettings()->imageUploaderHeaders = input["headers"].toString();
getSettings()->imageUploaderLink = input["link"].toString();
getSettings()->imageUploaderDeletionLink = input["deletionLink"].toString();
auto output = detail::exportSettings(*getSettings());
ASSERT_TRUE(fixture->run(output, UPDATE_SNAPSHOTS));
}
INSTANTIATE_TEST_SUITE_P(
ImageUploader, ExportTest,
testing::ValuesIn(testlib::Snapshot::discover("ImageUploader/Export")));
// verify that all snapshots are included
TEST(ImageUploader, ImportExportIntegrity)
{
ASSERT_FALSE(UPDATE_SNAPSHOTS); // make sure fixtures are actually tested
}
} // namespace chatterino::imageuploader::detail