|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include "fetchers.hh" |
| 3 | +#include "fetch-settings.hh" |
| 4 | +#include "json-utils.hh" |
| 5 | +#include <nlohmann/json.hpp> |
| 6 | +#include "tests/characterization.hh" |
| 7 | + |
| 8 | +namespace nix::fetchers { |
| 9 | + |
| 10 | +using nlohmann::json; |
| 11 | + |
| 12 | +class AccessKeysTest : public ::testing::Test |
| 13 | +{ |
| 14 | +protected: |
| 15 | + |
| 16 | +public: |
| 17 | + void SetUp() override { |
| 18 | + experimentalFeatureSettings.experimentalFeatures.get().insert(Xp::Flakes); |
| 19 | + } |
| 20 | + void TearDown() override { } |
| 21 | +}; |
| 22 | + |
| 23 | +TEST_F(AccessKeysTest, singleGitHub) |
| 24 | +{ |
| 25 | + fetchers::Settings fetchSettings = fetchers::Settings{}; |
| 26 | + fetchSettings.accessTokens.get().insert({"github.com","token"}); |
| 27 | + auto i = Input::fromURL(fetchSettings, "github:a/b"); |
| 28 | + |
| 29 | + auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b"); |
| 30 | + ASSERT_EQ(token,"token"); |
| 31 | +} |
| 32 | + |
| 33 | +TEST_F(AccessKeysTest, repoGitHub) |
| 34 | +{ |
| 35 | + fetchers::Settings fetchSettings = fetchers::Settings{}; |
| 36 | + fetchSettings.accessTokens.get().insert({"github.com","token"}); |
| 37 | + fetchSettings.accessTokens.get().insert({"github.com/a/b","another_token"}); |
| 38 | + fetchSettings.accessTokens.get().insert({"github.com/a/c","yet_another_token"}); |
| 39 | + auto i = Input::fromURL(fetchSettings, "github:a/a"); |
| 40 | + |
| 41 | + auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/a"); |
| 42 | + ASSERT_EQ(token,"token"); |
| 43 | + |
| 44 | + token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b"); |
| 45 | + ASSERT_EQ(token,"another_token"); |
| 46 | + |
| 47 | + token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/c"); |
| 48 | + ASSERT_EQ(token,"yet_another_token"); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_F(AccessKeysTest, multipleGitLab) |
| 52 | +{ |
| 53 | + fetchers::Settings fetchSettings = fetchers::Settings{}; |
| 54 | + fetchSettings.accessTokens.get().insert({"gitlab.com","token"}); |
| 55 | + fetchSettings.accessTokens.get().insert({"gitlab.com/a/b","another_token"}); |
| 56 | + auto i = Input::fromURL(fetchSettings, "gitlab:a/b"); |
| 57 | + |
| 58 | + auto token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/a/b"); |
| 59 | + ASSERT_EQ(token,"another_token"); |
| 60 | + |
| 61 | + token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/a/c"); |
| 62 | + ASSERT_EQ(token,"token"); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(AccessKeysTest, multipleSourceHut) |
| 66 | +{ |
| 67 | + fetchers::Settings fetchSettings = fetchers::Settings{}; |
| 68 | + fetchSettings.accessTokens.get().insert({"git.sr.ht","token"}); |
| 69 | + fetchSettings.accessTokens.get().insert({"git.sr.ht/~a/b","another_token"}); |
| 70 | + auto i = Input::fromURL(fetchSettings, "sourcehut:a/b"); |
| 71 | + |
| 72 | + auto token = i.scheme->getAccessToken(fetchSettings, "git.sr.ht", "git.sr.ht/~a/b"); |
| 73 | + ASSERT_EQ(token,"another_token"); |
| 74 | + |
| 75 | + token = i.scheme->getAccessToken(fetchSettings, "git.sr.ht", "git.sr.ht/~a/c"); |
| 76 | + ASSERT_EQ(token,"token"); |
| 77 | +} |
| 78 | + |
| 79 | +} |
0 commit comments