Size: 8966 bytes.


  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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// cs/apps/trycopilot.ai/api/auth_test.gpt.cc
#include "cs/apps/trycopilot.ai/api/auth.hh"

#include <string>

#include "cs/apps/trycopilot.ai/api/user.hh"
#include "cs/apps/trycopilot.ai/protos/auth.proto.hh"
#include "cs/apps/trycopilot.ai/protos/gencode/auth.proto.hh"
#include "cs/apps/trycopilot.ai/protos/gencode/user.proto.hh"
#include "cs/apps/trycopilot.ai/protos/user.proto.hh"
#include "cs/net/proto/db/client.gpt.hh"
#include "cs/net/proto/db/database_base_url.gpt.hh"
#include "cs/net/proto/db/in_memory_client.gpt.hh"
#include "cs/net/proto/db/query_helpers.gpt.hh"
#include "cs/net/proto/db/query_view.gpt.hh"
#include "cs/util/di/context.gpt.hh"
#include "cs/util/uuid.hh"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace {  // use_usings
using ::cs::apps::trycopilotai::api::CreateUserRPC;
using ::cs::apps::trycopilotai::api::LoginRPC;
using ::cs::apps::trycopilotai::api::LogoutRPC;
using ::cs::apps::trycopilotai::protos::CreateUserRequest;
using ::cs::apps::trycopilotai::protos::CreateUserResponse;
using ::cs::apps::trycopilotai::protos::LoginRequest;
using ::cs::apps::trycopilotai::protos::LoginResponse;
using ::cs::apps::trycopilotai::protos::LogoutRequest;
using ::cs::apps::trycopilotai::protos::LogoutResponse;
using ::cs::apps::trycopilotai::protos::Token;
using ::cs::apps::trycopilotai::protos::User;
using ::cs::net::proto::db::DatabaseBaseUrl;
using ::cs::net::proto::db::EQUALS;
using ::cs::net::proto::db::IDatabaseClient;
using ::cs::net::proto::db::InMemoryDatabaseClient;
using ::cs::net::proto::db::QueryView;
using ::cs::util::di::Context;
using ::cs::util::di::ContextBuilder;
using ::cs::util::random::uuid::generate_uuid_v4;
}  // namespace

using AppContext =
    Context<DatabaseBaseUrl, IDatabaseClient>;

// Test fixture for auth tests with in-memory DB via DI
class AuthAPITest : public ::testing::Test {
 protected:
  void SetUp() override {
    app_ctx_ = ContextBuilder<AppContext>()
                   .bind<DatabaseBaseUrl>()
                   .with(std::string(""))
                   .bind<IDatabaseClient>()
                   .to<InMemoryDatabaseClient>()
                   .build();
  }

  CreateUserRequest BuildCreateUserRequest(
      const std::string& email, const std::string& password,
      const std::string& confirm_password) {
    CreateUserRequest req;
    req.email = email;
    req.password = password;
    req.confirm_password = confirm_password;
    return req;
  }

  LoginRequest BuildLoginRequest(
      const std::string& email,
      const std::string& password) {
    LoginRequest req;
    req.email = email;
    req.password = password;
    return req;
  }

  AppContext app_ctx_;
  std::string test_email_ =
      "test_" + generate_uuid_v4() + "@example.com";
  std::string test_password_ = "SecureP@ssw0rd!";
};

TEST_F(AuthAPITest, CreateUserSuccess) {
  CreateUserRPC api(app_ctx_);
  auto request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);

  auto result = api.Process(request);
  ASSERT_TRUE(result.ok()) << result.message();
  EXPECT_EQ(result.value().email, test_email_);
  EXPECT_FALSE(result.value().user_uuid.empty());
}

TEST_F(AuthAPITest, CreateUserRejectsEmptyEmail) {
  CreateUserRPC api(app_ctx_);
  auto request = BuildCreateUserRequest("", test_password_,
                                        test_password_);

  auto result = api.Process(request);

  ASSERT_FALSE(result.ok());
  EXPECT_THAT(
      result.message(),
      ::testing::HasSubstr("Email must not be empty"));
}

TEST_F(AuthAPITest, CreateUserRejectsEmptyPassword) {
  CreateUserRPC api(app_ctx_);
  auto request =
      BuildCreateUserRequest(test_email_, "", "");

  auto result = api.Process(request);

  ASSERT_FALSE(result.ok());
  EXPECT_THAT(
      result.message(),
      ::testing::HasSubstr("Password must not be empty"));
}

TEST_F(AuthAPITest, CreateUserRejectsPasswordMismatch) {
  CreateUserRPC api(app_ctx_);
  auto request = BuildCreateUserRequest(
      test_email_, test_password_, "DifferentPassword");

  auto result = api.Process(request);

  ASSERT_FALSE(result.ok());
  EXPECT_THAT(
      result.message(),
      ::testing::HasSubstr("Passwords do not match"));
}

TEST_F(AuthAPITest, CreateUserRejectsDuplicateEmail) {
  CreateUserRPC api(app_ctx_);
  auto request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);

  auto first_result = api.Process(request);
  ASSERT_TRUE(first_result.ok()) << first_result.message();

  auto duplicate_result = api.Process(request);

  ASSERT_FALSE(duplicate_result.ok());
  EXPECT_THAT(duplicate_result.message(),
              ::testing::HasSubstr(
                  "User with this email already exists"));
}

TEST_F(AuthAPITest, LoginSuccessAfterRegistration) {
  CreateUserRPC create_api(app_ctx_);
  auto create_request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);
  auto create_result = create_api.Process(create_request);
  ASSERT_TRUE(create_result.ok())
      << create_result.message();

  LoginRPC login_rpc(app_ctx_);
  auto login_request =
      BuildLoginRequest(test_email_, test_password_);
  auto login_result = login_rpc.Process(login_request);

  ASSERT_TRUE(login_result.ok())
      << "Login failed: " << login_result.message();
  EXPECT_EQ(login_result.value().email, test_email_);
  EXPECT_FALSE(login_result.value().token.uuid.empty());
  EXPECT_EQ(login_result.value().token.user_uuid,
            create_result.value().user_uuid);
  EXPECT_TRUE(login_result.value().token.active);
}

TEST_F(AuthAPITest, LoginRejectsWrongPassword) {
  CreateUserRPC create_api(app_ctx_);
  auto create_request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);
  auto create_result = create_api.Process(create_request);
  ASSERT_TRUE(create_result.ok())
      << create_result.message();

  LoginRPC login_rpc(app_ctx_);
  auto login_request =
      BuildLoginRequest(test_email_, "WrongPassword");
  auto login_result = login_rpc.Process(login_request);

  ASSERT_FALSE(login_result.ok());
  EXPECT_THAT(login_result.message(),
              ::testing::HasSubstr("Invalid password"));
}

TEST_F(AuthAPITest, LoginRejectsNonexistentUser) {
  LoginRPC login_rpc(app_ctx_);
  auto login_request = BuildLoginRequest(
      "nonexistent_" + generate_uuid_v4() + "@example.com",
      test_password_);

  auto login_result = login_rpc.Process(login_request);

  ASSERT_FALSE(login_result.ok());
  EXPECT_THAT(login_result.message(),
              ::testing::HasSubstr("No values found"));
}

TEST_F(AuthAPITest, LoginReusesExistingActiveToken) {
  CreateUserRPC create_api(app_ctx_);
  auto create_request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);
  auto create_result = create_api.Process(create_request);
  ASSERT_TRUE(create_result.ok())
      << create_result.message();

  LoginRPC login_rpc(app_ctx_);
  auto login_request =
      BuildLoginRequest(test_email_, test_password_);
  auto first_login = login_rpc.Process(login_request);
  ASSERT_TRUE(first_login.ok()) << first_login.message();
  std::string first_token = first_login.value().token.uuid;

  auto second_login = login_rpc.Process(login_request);
  ASSERT_TRUE(second_login.ok()) << second_login.message();
  std::string second_token =
      second_login.value().token.uuid;

  EXPECT_EQ(first_token, second_token)
      << "Should reuse existing active token";
}

TEST_F(AuthAPITest, LogoutDeactivatesToken) {
  CreateUserRPC create_api(app_ctx_);
  auto create_request = BuildCreateUserRequest(
      test_email_, test_password_, test_password_);
  auto create_result = create_api.Process(create_request);
  ASSERT_TRUE(create_result.ok())
      << create_result.message();

  LoginRPC login_rpc(app_ctx_);
  auto login_request =
      BuildLoginRequest(test_email_, test_password_);
  auto login_result = login_rpc.Process(login_request);
  ASSERT_TRUE(login_result.ok()) << login_result.message();

  LogoutRPC logout_rpc(app_ctx_);
  LogoutRequest logout_request;
  logout_request.token_uuid =
      login_result.value().token.uuid;
  auto logout_result = logout_rpc.Process(logout_request);
  ASSERT_TRUE(logout_result.ok());
  EXPECT_TRUE(logout_result.value().logged_out);

  auto db = app_ctx_.Get<IDatabaseClient>();
  auto token_result =
      QueryView<Token>("tokens", db)
          .where(EQUALS(&Token::uuid,
                        login_result.value().token.uuid))
          .first();
  ASSERT_TRUE(token_result.ok());
  EXPECT_FALSE(token_result.value().active)
      << "Token should be inactive after logout";
}

TEST_F(AuthAPITest, LogoutRejectsEmptyTokenUuid) {
  LogoutRPC logout_rpc(app_ctx_);
  LogoutRequest logout_request;
  logout_request.token_uuid = "";

  auto result = logout_rpc.Process(logout_request);

  ASSERT_FALSE(result.ok());
  EXPECT_THAT(result.message(),
              ::testing::HasSubstr(
                  "token_uuid` must not be empty"));
}
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 1ms.