@@ -228,7 +228,7 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
228228 borrowingID int
229229 approve bool
230230 message string
231- setupMock func (transactionRepo * mock_domain.MockTransactionRepository )
231+ setupMock func (ownershipRepo * mock_domain. MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository )
232232 expectedTransaction * domain.Transaction
233233 expectedError error
234234 }{
@@ -239,7 +239,7 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
239239 borrowingID : 1 ,
240240 approve : true ,
241241 message : "approved" ,
242- setupMock : func (transactionRepo * mock_domain.MockTransactionRepository ) {
242+ setupMock : func (ownershipRepo * mock_domain. MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository ) {
243243 transactionRepo .EXPECT ().
244244 GetByID (1 ).
245245 Return (& domain.Transaction {
@@ -248,6 +248,12 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
248248 OwnershipID : 1 ,
249249 Status : domain .BorrowingStatusRequested ,
250250 }, nil )
251+ ownershipRepo .EXPECT ().
252+ GetByID (1 ).
253+ Return (& domain.Ownership {
254+ ID : 1 ,
255+ UserID : "user1" ,
256+ }, nil )
251257 transactionRepo .EXPECT ().
252258 Update (gomock .Any ()).
253259 DoAndReturn (func (tr * domain.Transaction ) (* domain.Transaction , error ) {
@@ -272,7 +278,7 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
272278 borrowingID : 1 ,
273279 approve : false ,
274280 message : "rejected" ,
275- setupMock : func (transactionRepo * mock_domain.MockTransactionRepository ) {
281+ setupMock : func (ownershipRepo * mock_domain. MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository ) {
276282 transactionRepo .EXPECT ().
277283 GetByID (1 ).
278284 Return (& domain.Transaction {
@@ -281,6 +287,12 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
281287 OwnershipID : 1 ,
282288 Status : domain .BorrowingStatusRequested ,
283289 }, nil )
290+ ownershipRepo .EXPECT ().
291+ GetByID (1 ).
292+ Return (& domain.Ownership {
293+ ID : 1 ,
294+ UserID : "user1" ,
295+ }, nil )
284296 transactionRepo .EXPECT ().
285297 Update (gomock .Any ()).
286298 DoAndReturn (func (tr * domain.Transaction ) (* domain.Transaction , error ) {
@@ -305,7 +317,7 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
305317 borrowingID : 1 ,
306318 approve : true ,
307319 message : "approved" ,
308- setupMock : func (transactionRepo * mock_domain.MockTransactionRepository ) {
320+ setupMock : func (ownershipRepo * mock_domain. MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository ) {
309321 transactionRepo .EXPECT ().
310322 GetByID (1 ).
311323 Return (nil , domain .ErrNotFound )
@@ -320,7 +332,7 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
320332 borrowingID : 1 ,
321333 approve : true ,
322334 message : "approved" ,
323- setupMock : func (transactionRepo * mock_domain.MockTransactionRepository ) {
335+ setupMock : func (ownershipRepo * mock_domain. MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository ) {
324336 transactionRepo .EXPECT ().
325337 GetByID (1 ).
326338 Return (& domain.Transaction {
@@ -329,22 +341,55 @@ func TestBorrowingUseCase_ReplyRequest(t *testing.T) {
329341 OwnershipID : 1 ,
330342 Status : domain .BorrowingStatusBorrowed ,
331343 }, nil )
344+ ownershipRepo .EXPECT ().
345+ GetByID (1 ).
346+ Return (& domain.Ownership {
347+ ID : 1 ,
348+ UserID : "user1" ,
349+ }, nil )
332350 },
333351 expectedTransaction : nil ,
334352 expectedError : domain .ErrInvalidTransactionStatus ,
335353 },
354+ {
355+ name : "failure: ownership owner mismatch" ,
356+ userID : "user2" ,
357+ ownershipID : 1 ,
358+ borrowingID : 1 ,
359+ approve : true ,
360+ message : "approved" ,
361+ setupMock : func (ownershipRepo * mock_domain.MockOwnershipRepository , transactionRepo * mock_domain.MockTransactionRepository ) {
362+ transactionRepo .EXPECT ().
363+ GetByID (1 ).
364+ Return (& domain.Transaction {
365+ ID : 1 ,
366+ UserID : "borrower" ,
367+ OwnershipID : 1 ,
368+ Status : domain .BorrowingStatusRequested ,
369+ }, nil )
370+ ownershipRepo .EXPECT ().
371+ GetByID (1 ).
372+ Return (& domain.Ownership {
373+ ID : 1 ,
374+ UserID : "user1" ,
375+ }, nil )
376+ },
377+ expectedTransaction : nil ,
378+ expectedError : ErrForbidden ,
379+ },
336380 }
337381
338382 for _ , tc := range testCases {
339383 t .Run (tc .name , func (t * testing.T ) {
340384 ctrl := gomock .NewController (t )
341385 defer ctrl .Finish ()
342386
387+ ownershipRepo := mock_domain .NewMockOwnershipRepository (ctrl )
343388 transactionRepo := mock_domain .NewMockTransactionRepository (ctrl )
344389
345- tc .setupMock (transactionRepo )
390+ tc .setupMock (ownershipRepo , transactionRepo )
346391
347- u := NewBorrowingUseCase (transactionRepo , nil )
392+ u := NewBorrowingUseCase (transactionRepo , ownershipRepo )
348393 transaction , err := u .ReplyRequest (tc .userID , tc .ownershipID , tc .borrowingID , tc .approve , tc .message )
349394
350395 if tc .expectedError != nil {
0 commit comments