github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/community_user_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"sort"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/DapperCollectives/CAST/backend/main/models"
    11  	"github.com/DapperCollectives/CAST/backend/tests/test_utils"
    12  	utils "github.com/DapperCollectives/CAST/backend/tests/test_utils"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  ////////////////////
    17  // CommunityUsers //
    18  ////////////////////
    19  
    20  func TestCreateCommunityUsers(t *testing.T) {
    21  	clearTable("communities")
    22  	clearTable("community_users")
    23  
    24  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
    25  
    26  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    27  
    28  	response := otu.CreateCommunityAPI(communityPayload)
    29  	checkResponseCode(t, http.StatusCreated, response.Code)
    30  
    31  	//Parse Community
    32  	var community models.Community
    33  	json.Unmarshal(response.Body.Bytes(), &community)
    34  
    35  	//Query the community
    36  	response = otu.GetCommunityUsersAPI(community.ID)
    37  	checkResponseCode(t, http.StatusOK, response.Code)
    38  
    39  	//Parse the response
    40  	var p test_utils.PaginatedResponseWithUser
    41  	json.Unmarshal(response.Body.Bytes(), &p)
    42  
    43  	for _, user := range p.Data {
    44  		if utils.DefaultAuthor == user {
    45  			assert.Equal(t, utils.DefaultAuthor, user)
    46  		}
    47  	}
    48  }
    49  
    50  func TestGetCommunityUsers(t *testing.T) {
    51  	clearTable("communities")
    52  	clearTable("community_users")
    53  
    54  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
    55  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    56  
    57  	response := otu.CreateCommunityAPI(communityPayload)
    58  	checkResponseCode(t, http.StatusCreated, response.Code)
    59  
    60  	var community models.Community
    61  	json.Unmarshal(response.Body.Bytes(), &community)
    62  
    63  	response = otu.GetCommunityUsersAPI(community.ID)
    64  	checkResponseCode(t, http.StatusOK, response.Code)
    65  
    66  	var p test_utils.PaginatedResponseWithUserType
    67  	json.Unmarshal(response.Body.Bytes(), &p)
    68  
    69  	assert.Equal(t, 1, len(p.Data))
    70  	assert.Equal(t, true, p.Data[0].Is_admin)
    71  	assert.Equal(t, true, p.Data[0].Is_author)
    72  	assert.Equal(t, true, p.Data[0].Is_member)
    73  }
    74  
    75  func TestGetCommunityUsersByType(t *testing.T) {
    76  	clearTable("communities")
    77  	clearTable("community_users")
    78  
    79  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
    80  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    81  
    82  	response := otu.CreateCommunityAPI(communityPayload)
    83  	checkResponseCode(t, http.StatusCreated, response.Code)
    84  
    85  	var community models.Community
    86  	json.Unmarshal(response.Body.Bytes(), &community)
    87  
    88  	response = otu.GetCommunityUsersAPIByType(community.ID, "admin")
    89  	checkResponseCode(t, http.StatusOK, response.Code)
    90  
    91  	var p test_utils.PaginatedResponseWithUser
    92  	json.Unmarshal(response.Body.Bytes(), &p)
    93  
    94  	assert.Equal(t, 1, len(p.Data))
    95  	assert.Equal(t, "admin", p.Data[0].User_type)
    96  
    97  	response = otu.GetCommunityUsersAPIByType(community.ID, "author")
    98  	checkResponseCode(t, http.StatusOK, response.Code)
    99  
   100  	json.Unmarshal(response.Body.Bytes(), &p)
   101  
   102  	assert.Equal(t, 1, len(p.Data))
   103  	assert.Equal(t, "author", p.Data[0].User_type)
   104  
   105  	response = otu.GetCommunityUsersAPIByType(community.ID, "member")
   106  	checkResponseCode(t, http.StatusOK, response.Code)
   107  
   108  	json.Unmarshal(response.Body.Bytes(), &p)
   109  
   110  	assert.Equal(t, 1, len(p.Data))
   111  	assert.Equal(t, "member", p.Data[0].User_type)
   112  }
   113  
   114  func TestGetCommunityUsersByInvalidType(t *testing.T) {
   115  	clearTable("communities")
   116  	clearTable("community_users")
   117  
   118  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   119  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   120  
   121  	response := otu.CreateCommunityAPI(communityPayload)
   122  	checkResponseCode(t, http.StatusCreated, response.Code)
   123  
   124  	var community models.Community
   125  	json.Unmarshal(response.Body.Bytes(), &community)
   126  
   127  	response = otu.GetCommunityUsersAPIByType(community.ID, "invalidType")
   128  	checkResponseCode(t, http.StatusBadRequest, response.Code)
   129  }
   130  
   131  func TestGetUserCommunities(t *testing.T) {
   132  	clearTable("communities")
   133  	clearTable("community_users")
   134  
   135  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   136  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   137  
   138  	response := otu.CreateCommunityAPI(communityPayload)
   139  	checkResponseCode(t, http.StatusCreated, response.Code)
   140  
   141  	var community models.Community
   142  	json.Unmarshal(response.Body.Bytes(), &community)
   143  
   144  	response = otu.GetUserCommunitiesAPI(utils.AdminAddr)
   145  	checkResponseCode(t, http.StatusOK, response.Code)
   146  
   147  	var p test_utils.PaginatedResponseWithUserCommunity
   148  	json.Unmarshal(response.Body.Bytes(), &p)
   149  
   150  	roles := strings.Split(p.Data[0].Roles, ",")
   151  	sort.Strings(roles)
   152  
   153  	assert.Equal(t, 1, p.TotalRecords)
   154  	assert.Equal(t, "admin,author,member", strings.Join(roles, ","))
   155  }
   156  
   157  func TestDeleteUserFromCommunity(t *testing.T) {
   158  	clearTable("communities")
   159  	clearTable("community_users")
   160  
   161  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   162  
   163  	//create the author before generating the payload
   164  	var createCommunityStruct models.CreateCommunityRequestPayload
   165  	createCommunityStruct.Community = *communityStruct
   166  	author := []string{"0x01cf0e2f2f715450"}
   167  	createCommunityStruct.Additional_authors = &author
   168  
   169  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   170  
   171  	response := otu.CreateCommunityAPI(communityPayload)
   172  	checkResponseCode(t, http.StatusCreated, response.Code)
   173  
   174  	var community models.Community
   175  	json.Unmarshal(response.Body.Bytes(), &community)
   176  
   177  	//generate the user, admin must be the signer
   178  	userStruct := otu.GenerateCommunityUserStruct("user1", "author")
   179  	userPayload := otu.GenerateCommunityUserPayload("account", userStruct)
   180  
   181  	response = otu.DeleteUserFromCommunityAPI(
   182  		community.ID,
   183  		utils.UserOneAddr,
   184  		"author",
   185  		userPayload,
   186  	)
   187  	checkResponseCode(t, http.StatusOK, response.Code)
   188  
   189  	//Query the community
   190  	response = otu.GetCommunityUsersAPI(community.ID)
   191  	checkResponseCode(t, http.StatusOK, response.Code)
   192  
   193  	//Parse the response
   194  	var p test_utils.PaginatedResponseWithUser
   195  	json.Unmarshal(response.Body.Bytes(), &p)
   196  
   197  	// assert that the user does not have the role of author
   198  	for _, user := range p.Data {
   199  		if user.Addr == utils.UserOneAddr {
   200  			assert.False(t, user.User_type == "author")
   201  		}
   202  	}
   203  }