1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_ACCOUNT_REPOSITORY_H_
#define COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_ACCOUNT_REPOSITORY_H_
#include <memory>
#include <optional>
#include <string>
#include "base/files/file_util.h"
#include "base/json/json_value_converter.h"
#include "components/supervised_user/core/browser/proto/kidsmanagement_messages.pb.h"
namespace supervised_user {
// Entities for test account repository.
// example JSON: {"families": [{"members": [{"role": "PARENT", "username":
// "joe@gmail.com", "password": "123"}], "feature": "REGULAR"}]}
namespace test_accounts {
enum class Feature : int {
REGULAR,
DMA_ELIGIBLE_WITH_CONSENT,
DMA_ELIGIBLE_WITHOUT_CONSENT,
DMA_INELIGIBLE,
};
// Converts a string literal to `Feature` value.
std::optional<Feature> ParseFeature(std::string_view feature_name);
struct FamilyMember {
static void RegisterJSONConverter(
base::JSONValueConverter<FamilyMember>* converter);
static bool ParseRole(std::string_view value,
kidsmanagement::FamilyRole* role);
// Struct is complex.
FamilyMember();
FamilyMember(const FamilyMember&);
FamilyMember& operator=(const FamilyMember&);
~FamilyMember();
std::string name;
kidsmanagement::FamilyRole role;
std::string username;
std::string password;
};
struct Family {
static void RegisterJSONConverter(
base::JSONValueConverter<Family>* converter);
static bool ParseFeature(std::string_view value, Feature* feature);
// Struct is complex.
Family();
Family(const Family& other);
Family& operator=(const Family& other);
~Family();
std::vector<std::unique_ptr<FamilyMember>> members;
Feature feature;
};
struct Repository {
static void RegisterJSONConverter(
base::JSONValueConverter<Repository>* converter);
// Struct is complex.
Repository();
Repository(const Repository& other);
Repository& operator=(const Repository& other);
~Repository();
std::vector<std::unique_ptr<Family>> families;
};
} // namespace test_accounts
// Returns first family member with requested role.
std::optional<test_accounts::FamilyMember> GetFirstFamilyMemberByRole(
const test_accounts::Family& family,
kidsmanagement::FamilyRole role);
// File-based repository of test accounts. Balances access to the accounts.
class TestAccountRepository {
public:
// Creates the repository from the path. If the path is relative, it will be
// based on DIR_SRC_TEST_DATA_ROOT.
explicit TestAccountRepository(const base::FilePath& path);
~TestAccountRepository();
TestAccountRepository(const TestAccountRepository&) = delete;
TestAccountRepository& operator=(const TestAccountRepository&) = delete;
// Returns random family that satisfies the feature if it exists.
// Randomness is on purpose: tests can't rely on a specific family by order.
// Instead, they should pick a family using a required feature.
std::optional<test_accounts::Family> GetRandomFamilyByFeature(
test_accounts::Feature feature);
private:
test_accounts::Repository repository_;
};
} // namespace supervised_user
#endif // COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_ACCOUNT_REPOSITORY_H_
|