From fd551f52831663d7f6fce2ec47afc6cd03e88b82 Mon Sep 17 00:00:00 2001
From: birkon <birkon@stud.ntnu.no>
Date: Thu, 4 May 2023 14:35:18 +0200
Subject: [PATCH] added test for add/remove from group and open/close group

---
 .../controller/group/GroupControllerTest.java | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java
index e1682ff3..1cbf6abe 100644
--- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java
+++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java
@@ -1,6 +1,7 @@
 package ntnu.idatt2016.v233.SmartMat.controller.group;
 
 import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
+import ntnu.idatt2016.v233.SmartMat.dto.request.group.ChangeAuthorityRequest;
 import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupDetailsResponse;
 import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
 import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
@@ -230,6 +231,58 @@ public class GroupControllerTest {
         assertNotNull(groupResponseEntity.getBody());
         assertEquals("User removed successfully.", groupResponseEntity.getBody());
     }
+
+    @Test
+    void shouldChangeAuthority(){
+        when(groupService.getGroupById(group.getGroupId())).thenReturn(Optional.ofNullable(group));
+        when(groupService.isUserAssociatedWithGroup(regularUser.getName(), group.getGroupId())).thenReturn(true);
+        when(groupService.getUserGroupAssoAuthority(regularUser.getName(), group.getGroupId())).thenReturn("ADMIN");
+
+        when(userService.getUserFromUsername(regularUser.getName())).thenReturn(Optional.ofNullable(user));
+
+        ChangeAuthorityRequest changeAuthorityRequest = new ChangeAuthorityRequest(user.getUsername(),
+                group.getGroupId(), "USER");
+
+        ResponseEntity<?> groupResponseEntity =
+                groupController.changeAuthority(changeAuthorityRequest, regularUser);
+
+        assertSame(HttpStatus.OK, groupResponseEntity.getStatusCode());
+        assertNotNull(groupResponseEntity.getBody());
+        assertEquals("Authority changed successfully.", groupResponseEntity.getBody());
+
+        when(groupService.getUserGroupAssoAuthority(regularUser.getName(), group.getGroupId())).thenReturn("USER");
+
+        groupResponseEntity =
+                groupController.changeAuthority(changeAuthorityRequest, regularUser);
+
+        assertSame(HttpStatus.FORBIDDEN, groupResponseEntity.getStatusCode());
+        assertNull(groupResponseEntity.getBody());
+
+    }
+
+    @Test
+    void shouldChangeOpen(){
+        when(groupService.isUserAssociatedWithGroup(regularUser.getName(), group.getGroupId())).thenReturn(true);
+        when(groupService.getUserGroupAssoAuthority(regularUser.getName(), group.getGroupId())).thenReturn("ADMIN");
+
+
+        when(groupService.OpenOrCloseGroup(group.getGroupId())).thenReturn(Optional.of(true));
+
+        ResponseEntity<?> groupResponseEntity =
+                groupController.changeOpenValue(group.getGroupId(), regularUser);
+
+        assertSame(HttpStatus.OK, groupResponseEntity.getStatusCode());
+        assertNotNull(groupResponseEntity.getBody());
+        assertTrue((boolean) groupResponseEntity.getBody());
+
+        when(groupService.getUserGroupAssoAuthority(regularUser.getName(), group.getGroupId())).thenReturn("USER");
+
+        groupResponseEntity =
+                groupController.changeOpenValue(group.getGroupId(), regularUser);
+
+        assertSame(HttpStatus.FORBIDDEN, groupResponseEntity.getStatusCode());
+        assertNull(groupResponseEntity.getBody());
+    }
     
 
 }
\ No newline at end of file
-- 
GitLab