Leosac  0.8.0
Open Source Access Control
GroupValidator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "core/auth/Group.hpp"
21 #include "core/auth/User.hpp"
23 #include "gtest/gtest.h"
24 
25 using namespace Leosac;
26 using namespace Leosac::Auth;
27 
28 namespace Leosac
29 {
30 namespace Test
31 {
32 
33 TEST(TestGroup, memberships)
34 {
35  auto user = std::make_shared<User>("toto");
36  auto group = std::make_shared<Group>("my_group");
37 
38  auto in_grp = [](UserPtr u, GroupPtr g) {
39  auto members = g->members();
40  for (const auto &member : members)
41  {
42  if (member->username() == u->username())
43  return true;
44  }
45  return false;
46  };
47  ASSERT_FALSE(in_grp(user, group));
48  group->member_add(user);
49  ASSERT_TRUE(in_grp(user, group));
50 }
51 
52 TEST(TestGroupValidator, name_length)
53 {
54  Group g;
55  ASSERT_THROW({ g.name("s"); }, ModelException);
56 
57 
58  ASSERT_NO_THROW({
59  g.name("long_enough");
61  });
62 
63  ASSERT_THROW(
64  { g.name("this_is_so_long_this_name_is_clearly_to_long_to_be_valid"); },
66 }
67 
68 TEST(TestGroupValidator, name_invalid_char)
69 {
70  Group g;
71  ASSERT_NO_THROW({ g.name("aaaa"); });
72  ASSERT_THROW({ g.name("aaa$"); }, ModelException);
73  ASSERT_THROW({ g.name("aaa!"); }, ModelException);
74  ASSERT_THROW({ g.name("aaa^"); }, ModelException);
75  ASSERT_THROW({ g.name("aaa^"); }, ModelException);
76  ASSERT_THROW({ g.name("aaaƤ"); }, ModelException);
77  ASSERT_THROW({ g.name("aaaĆ©"); }, ModelException);
78  ASSERT_THROW({ g.name("aaa aaa"); }, ModelException);
79 }
80 }
81 }
Leosac::Test::TEST
TEST(TestGroupValidator, name_invalid_char)
Definition: GroupValidator.cpp:68
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
User.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Group.hpp
Leosac::Auth::GroupPtr
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
Leosac::Auth::Group
A authentication group regroup users that share permissions.
Definition: Group.hpp:57
ModelException.hpp
Leosac::Auth::Group::name
const std::string & name() const
Definition: Group.cpp:41
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Auth::GroupValidator::validate
static void validate(const Group &grp)
Validate the group's attributes.
Definition: Group.cpp:167