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

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/DapperCollectives/CAST/backend/main/models"
     9  	"github.com/DapperCollectives/CAST/backend/main/shared"
    10  	"github.com/DapperCollectives/CAST/backend/tests/test_utils"
    11  	utils "github.com/DapperCollectives/CAST/backend/tests/test_utils"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  /*********************/
    16  /*     COMMUNITIES   */
    17  /*********************/
    18  
    19  func TestEmptyCommunityTable(t *testing.T) {
    20  	clearTable("communities")
    21  
    22  	req, _ := http.NewRequest("GET", "/communities", nil)
    23  	response := executeRequest(req)
    24  
    25  	checkResponseCode(t, http.StatusOK, response.Code)
    26  	var body shared.PaginatedResponse
    27  	json.Unmarshal(response.Body.Bytes(), &body)
    28  
    29  	if body.Count != 0 {
    30  		t.Errorf("Expected empty body. Got count of %d", body.Count)
    31  	}
    32  }
    33  
    34  func TestGetNonExistentCommunity(t *testing.T) {
    35  	clearTable("communities")
    36  
    37  	response := otu.GetCommunityAPI(420)
    38  	checkResponseCode(t, http.StatusBadRequest, response.Code)
    39  
    40  	var e errorResponse
    41  	json.Unmarshal(response.Body.Bytes(), &e)
    42  	assert.Equal(t, errIncompleteRequest, e)
    43  }
    44  
    45  func TestCreateCommunity(t *testing.T) {
    46  	// Prep
    47  	clearTable("communities")
    48  	clearTable("community_users")
    49  
    50  	// Create Community
    51  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
    52  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    53  
    54  	response := otu.CreateCommunityAPI(communityPayload)
    55  	checkResponseCode(t, http.StatusCreated, response.Code)
    56  
    57  	// Parse
    58  	var community models.Community
    59  	json.Unmarshal(response.Body.Bytes(), &community)
    60  
    61  	// Validate
    62  	assert.Equal(t, utils.DefaultCommunity.Name, community.Name)
    63  	assert.Equal(t, utils.DefaultCommunity.Body, community.Body)
    64  	assert.Equal(t, utils.DefaultCommunity.Logo, community.Logo)
    65  	assert.NotNil(t, community.ID)
    66  }
    67  
    68  func TestCreateCommunityFailStrategy(t *testing.T) {
    69  	// Prep
    70  	clearTable("communities")
    71  	clearTable("community_users")
    72  
    73  	// Create Community
    74  	communityStruct := otu.GenerateFailStrategyCommunityStruct("account")
    75  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    76  
    77  	response := otu.CreateCommunityAPI(communityPayload)
    78  	checkResponseCode(t, http.StatusBadRequest, response.Code)
    79  }
    80  
    81  func TestCreateCommunityFailThreshold(t *testing.T) {
    82  	// Prep
    83  	clearTable("communities")
    84  	clearTable("community_users")
    85  
    86  	// Create Community
    87  	communityStruct := otu.GenerateFailThresholdCommunityStruct("account")
    88  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
    89  
    90  	response := otu.CreateCommunityAPI(communityPayload)
    91  	checkResponseCode(t, http.StatusBadRequest, response.Code)
    92  }
    93  
    94  func TestCreateCommunityNilStrategy(t *testing.T) {
    95  	// Prep
    96  	clearTable("communities")
    97  	clearTable("community_users")
    98  
    99  	// Create Community
   100  	communityStruct := otu.GenerateNilStrategyCommunityStruct("account")
   101  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   102  
   103  	response := otu.CreateCommunityAPI(communityPayload)
   104  	checkResponseCode(t, http.StatusCreated, response.Code)
   105  
   106  	// Parse
   107  	var community models.Community
   108  	json.Unmarshal(response.Body.Bytes(), &community)
   109  
   110  	// Validate
   111  	assert.Equal(t, utils.NilStrategyCommunity.Name, community.Name)
   112  	assert.Equal(t, utils.NilStrategyCommunity.Body, community.Body)
   113  	assert.Equal(t, utils.NilStrategyCommunity.Logo, community.Logo)
   114  	assert.NotNil(t, community.ID)
   115  }
   116  
   117  func TestCommunityAdminRoles(t *testing.T) {
   118  	clearTable("communities")
   119  	clearTable("community_users")
   120  
   121  	//CreateCommunity
   122  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   123  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   124  
   125  	response := otu.CreateCommunityAPI(communityPayload)
   126  	checkResponseCode(t, http.StatusCreated, response.Code)
   127  
   128  	// Parse Community
   129  	var community models.Community
   130  	json.Unmarshal(response.Body.Bytes(), &community)
   131  
   132  	response = otu.GetCommunityUsersAPI(community.ID)
   133  	checkResponseCode(t, http.StatusOK, response.Code)
   134  
   135  	var p test_utils.PaginatedResponseWithUserType
   136  	json.Unmarshal(response.Body.Bytes(), &p)
   137  
   138  	//Admin user has all possible roles
   139  	assert.Equal(t, true, p.Data[0].Is_admin)
   140  	assert.Equal(t, true, p.Data[0].Is_author)
   141  	assert.Equal(t, true, p.Data[0].Is_member)
   142  }
   143  
   144  func TestCommunityAuthorRoles(t *testing.T) {
   145  	clearTable("communities")
   146  	clearTable("community_users")
   147  
   148  	//CreateCommunity
   149  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   150  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   151  
   152  	response := otu.CreateCommunityAPI(communityPayload)
   153  	checkResponseCode(t, http.StatusCreated, response.Code)
   154  
   155  	// Parse Community
   156  	var community models.Community
   157  	json.Unmarshal(response.Body.Bytes(), &community)
   158  
   159  	//Generate the user, admin must be the signer
   160  	userStruct := otu.GenerateCommunityUserStruct("user1", "author")
   161  	userPayload := otu.GenerateCommunityUserPayload("account", userStruct)
   162  
   163  	response = otu.CreateCommunityUserAPI(community.ID, userPayload)
   164  	checkResponseCode(t, http.StatusCreated, response.Code)
   165  
   166  	// Query the community
   167  	response = otu.GetCommunityUsersAPI(community.ID)
   168  	checkResponseCode(t, http.StatusOK, response.Code)
   169  
   170  	var p test_utils.PaginatedResponseWithUserType
   171  	json.Unmarshal(response.Body.Bytes(), &p)
   172  
   173  	assert.Equal(t, false, p.Data[0].Is_admin)
   174  	assert.Equal(t, true, p.Data[0].Is_author)
   175  	assert.Equal(t, true, p.Data[0].Is_member)
   176  }
   177  
   178  func TestGetCommunityAPI(t *testing.T) {
   179  	clearTable("communities")
   180  	clearTable("community_users")
   181  	otu.AddCommunities(1, "dao")
   182  
   183  	response := otu.GetCommunityAPI(1)
   184  
   185  	checkResponseCode(t, http.StatusOK, response.Code)
   186  }
   187  
   188  func TestGetCommunitiesForHomepageAPI(t *testing.T) {
   189  	clearTable("communities")
   190  	clearTable("community_users")
   191  	communityIds := otu.AddCommunities(2, "dao")
   192  	otu.MakeFeaturedCommunity(communityIds[0])
   193  
   194  	response := otu.GetCommunitiesForHomepageAPI()
   195  
   196  	checkResponseCode(t, http.StatusOK, response.Code)
   197  
   198  	//Parse the response
   199  	var p test_utils.PaginatedResponseWithCommunity
   200  	json.Unmarshal(response.Body.Bytes(), &p)
   201  
   202  	assert.Equal(t, 1, len(p.Data))
   203  }
   204  
   205  func TestSearchForCommunities(t *testing.T) {
   206  	clearTable("communities")
   207  	clearTable("community_users")
   208  	communityIds := otu.AddCommunities(5, "dao")
   209  	otu.MakeFeaturedCommunity(communityIds[0])
   210  	otu.MakeFeaturedCommunity(communityIds[1])
   211  
   212  	communityIds = otu.AddCommunities(3, "social")
   213  	otu.MakeFeaturedCommunity(communityIds[0])
   214  	otu.MakeFeaturedCommunity(communityIds[1])
   215  
   216  	communityIds = otu.AddCommunities(2, "protocol")
   217  	otu.MakeFeaturedCommunity(communityIds[0])
   218  
   219  	otu.AddCommunities(1, "Collector")
   220  
   221  	t.Run("Default Search for Featured Communities", func(t *testing.T) {
   222  		response := otu.GetSearchCommunitiesAPI([]string{}, "", nil)
   223  
   224  		checkResponseCode(t, http.StatusOK, response.Code)
   225  
   226  		var p test_utils.PaginatedResponseSearch
   227  		json.Unmarshal(response.Body.Bytes(), &p)
   228  
   229  		assert.Equal(t, 2, p.Filters[0].Amount)                // Dao
   230  		assert.Equal(t, 2, p.Filters[1].Amount)                // Social
   231  		assert.Equal(t, 1, p.Filters[2].Amount)                // Protocol
   232  		assert.Equal(t, 5, p.Filters[len(p.Filters)-1].Amount) // All
   233  		assert.Equal(t, 5, len(p.Results.Data))                // Featured Communities
   234  	})
   235  
   236  	t.Run("Default Search with filter", func(t *testing.T) {
   237  		response := otu.GetSearchCommunitiesAPI([]string{"social"}, "", nil)
   238  
   239  		checkResponseCode(t, http.StatusOK, response.Code)
   240  
   241  		var p1 test_utils.PaginatedResponseSearch
   242  		json.Unmarshal(response.Body.Bytes(), &p1)
   243  
   244  		assert.Equal(t, 2, p1.Filters[0].Amount)                 // Dao
   245  		assert.Equal(t, 2, p1.Filters[1].Amount)                 // Social
   246  		assert.Equal(t, 1, p1.Filters[2].Amount)                 // Protocol
   247  		assert.Equal(t, 5, p1.Filters[len(p1.Filters)-1].Amount) // All
   248  		assert.Equal(t, 2, len(p1.Results.Data))                 // Filtered by "social"
   249  
   250  		response = otu.GetSearchCommunitiesAPI([]string{"social,dao"}, "", nil)
   251  
   252  		checkResponseCode(t, http.StatusOK, response.Code)
   253  
   254  		var p2 test_utils.PaginatedResponseSearch
   255  		json.Unmarshal(response.Body.Bytes(), &p2)
   256  
   257  		assert.Equal(t, 2, p2.Filters[0].Amount)                 // Dao
   258  		assert.Equal(t, 2, p2.Filters[1].Amount)                 // Social
   259  		assert.Equal(t, 1, p2.Filters[2].Amount)                 // Protocol
   260  		assert.Equal(t, 5, p2.Filters[len(p2.Filters)-1].Amount) // All
   261  		assert.Equal(t, 4, len(p2.Results.Data))                 // Filtered by "social" and "dao"
   262  	})
   263  
   264  	t.Run("Limit Default Search", func(t *testing.T) {
   265  		limit := 2
   266  		response := otu.GetSearchCommunitiesAPI([]string{}, "", &limit)
   267  
   268  		checkResponseCode(t, http.StatusOK, response.Code)
   269  
   270  		var p test_utils.PaginatedResponseSearch
   271  		json.Unmarshal(response.Body.Bytes(), &p)
   272  
   273  		assert.Equal(t, 2, p.Filters[0].Amount)                // Dao
   274  		assert.Equal(t, 2, p.Filters[1].Amount)                // Social
   275  		assert.Equal(t, 1, p.Filters[2].Amount)                // Protocol
   276  		assert.Equal(t, 5, p.Filters[len(p.Filters)-1].Amount) // All
   277  		assert.Equal(t, limit, len(p.Results.Data))            // Featured Communities limited
   278  	})
   279  
   280  	t.Run("Limit Default Search with filter", func(t *testing.T) {
   281  		limit := 1
   282  		response := otu.GetSearchCommunitiesAPI([]string{"dao"}, "", &limit)
   283  
   284  		checkResponseCode(t, http.StatusOK, response.Code)
   285  
   286  		var p test_utils.PaginatedResponseSearch
   287  		json.Unmarshal(response.Body.Bytes(), &p)
   288  
   289  		assert.Equal(t, 2, p.Filters[0].Amount)                // Dao
   290  		assert.Equal(t, 2, p.Filters[1].Amount)                // Social
   291  		assert.Equal(t, 1, p.Filters[2].Amount)                // Protocol
   292  		assert.Equal(t, 5, p.Filters[len(p.Filters)-1].Amount) // All
   293  		assert.Equal(t, limit, len(p.Results.Data))            // Filtered and limited
   294  	})
   295  
   296  	t.Run("Search with text", func(t *testing.T) {
   297  		response := otu.GetSearchCommunitiesAPI([]string{}, "test", nil)
   298  
   299  		checkResponseCode(t, http.StatusOK, response.Code)
   300  
   301  		var p test_utils.PaginatedResponseSearch
   302  		json.Unmarshal(response.Body.Bytes(), &p)
   303  
   304  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   305  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   306  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   307  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   308  		assert.Equal(t, 10, len(p.Results.Data))                // text = "test"
   309  	})
   310  
   311  	t.Run("Search with text no results", func(t *testing.T) {
   312  		response := otu.GetSearchCommunitiesAPI([]string{}, "abc", nil)
   313  
   314  		checkResponseCode(t, http.StatusOK, response.Code)
   315  
   316  		var p test_utils.PaginatedResponseSearch
   317  		json.Unmarshal(response.Body.Bytes(), &p)
   318  
   319  		assert.Equal(t, 0, p.Filters[0].Amount)                // Dao
   320  		assert.Equal(t, 0, p.Filters[1].Amount)                // Social
   321  		assert.Equal(t, 0, p.Filters[2].Amount)                // Protocol
   322  		assert.Equal(t, 0, p.Filters[len(p.Filters)-1].Amount) // All
   323  		assert.Equal(t, 0, len(p.Results.Data))                // text = "abc"
   324  	})
   325  
   326  	t.Run("Search with text and filter", func(t *testing.T) {
   327  		response := otu.GetSearchCommunitiesAPI([]string{"dao"}, "test", nil)
   328  
   329  		checkResponseCode(t, http.StatusOK, response.Code)
   330  
   331  		var p test_utils.PaginatedResponseSearch
   332  		json.Unmarshal(response.Body.Bytes(), &p)
   333  
   334  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   335  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   336  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   337  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   338  		assert.Equal(t, 5, len(p.Results.Data))                 // text = "test"
   339  	})
   340  
   341  	t.Run("Search with text and multiple filters", func(t *testing.T) {
   342  		response := otu.GetSearchCommunitiesAPI([]string{"dao", "social"}, "test", nil)
   343  
   344  		checkResponseCode(t, http.StatusOK, response.Code)
   345  
   346  		var p test_utils.PaginatedResponseSearch
   347  		json.Unmarshal(response.Body.Bytes(), &p)
   348  
   349  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   350  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   351  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   352  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   353  		assert.Equal(t, 8, len(p.Results.Data))                 // text = "test"
   354  	})
   355  
   356  	t.Run("Limit Search with text", func(t *testing.T) {
   357  		limit := 5
   358  		response := otu.GetSearchCommunitiesAPI([]string{}, "test", &limit)
   359  
   360  		checkResponseCode(t, http.StatusOK, response.Code)
   361  
   362  		var p test_utils.PaginatedResponseSearch
   363  		json.Unmarshal(response.Body.Bytes(), &p)
   364  
   365  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   366  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   367  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   368  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   369  		assert.Equal(t, limit, len(p.Results.Data))             // limited to 5
   370  	})
   371  
   372  	t.Run("Limit Search with text and filter", func(t *testing.T) {
   373  		limit := 3
   374  		response := otu.GetSearchCommunitiesAPI([]string{"dao"}, "test", &limit)
   375  
   376  		checkResponseCode(t, http.StatusOK, response.Code)
   377  
   378  		var p test_utils.PaginatedResponseSearch
   379  		json.Unmarshal(response.Body.Bytes(), &p)
   380  
   381  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   382  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   383  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   384  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   385  		assert.Equal(t, limit, len(p.Results.Data))             // limited to 3
   386  	})
   387  
   388  	t.Run("Search Pagination", func(t *testing.T) {
   389  		limit := 3
   390  		response := otu.GetSearchCommunitiesAPI([]string{}, "test", &limit)
   391  
   392  		checkResponseCode(t, http.StatusOK, response.Code)
   393  
   394  		var p test_utils.PaginatedResponseSearch
   395  		json.Unmarshal(response.Body.Bytes(), &p)
   396  
   397  		assert.Equal(t, 5, p.Filters[0].Amount)                 // Dao
   398  		assert.Equal(t, 3, p.Filters[1].Amount)                 // Social
   399  		assert.Equal(t, 2, p.Filters[2].Amount)                 // Protocol
   400  		assert.Equal(t, 10, p.Filters[len(p.Filters)-1].Amount) // All
   401  		assert.Equal(t, limit, len(p.Results.Data))             // limited to 3
   402  	})
   403  
   404  	t.Run("Total Records should be the same as all field of filters", func(t *testing.T) {
   405  		limit := 10
   406  		response := otu.GetSearchCommunitiesAPI([]string{}, "test", &limit)
   407  
   408  		checkResponseCode(t, http.StatusOK, response.Code)
   409  
   410  		var p test_utils.PaginatedResponseSearch
   411  		json.Unmarshal(response.Body.Bytes(), &p)
   412  
   413  		assert.Equal(t, p.Results.TotalRecords, p.Filters[len(p.Filters)-1].Amount) // All
   414  	})
   415  
   416  }
   417  
   418  func TestGetCommunityActiveStrategies(t *testing.T) {
   419  	clearTable("communities")
   420  	clearTable("community_users")
   421  	clearTable("proposals")
   422  
   423  	communityId := otu.AddCommunitiesWithUsers(1, "user1")[0]
   424  
   425  	proposalStruct := otu.GenerateProposalStruct("user1", communityId)
   426  	payload1 := otu.GenerateProposalPayload("user1", proposalStruct)
   427  	otu.CreateProposalAPI(payload1)
   428  
   429  	payload2 := otu.GenerateProposalPayload("user1", proposalStruct)
   430  	otu.CreateProposalAPI(payload2)
   431  
   432  	cancelPayload := otu.GenerateCancelProposalStruct("user1", payload1.ID)
   433  	otu.UpdateProposalAPI(payload1.ID, cancelPayload)
   434  
   435  	response := otu.GetCommunityActiveStrategies(communityId)
   436  	CheckResponseCode(t, http.StatusOK, response.Code)
   437  
   438  	var results []string
   439  	json.Unmarshal(response.Body.Bytes(), &results)
   440  
   441  	assert.Equal(t, 1, len(results))
   442  }
   443  
   444  func TestUpdateCommunity(t *testing.T) {
   445  	clearTable("communities")
   446  	clearTable("community_users")
   447  
   448  	// Create Community
   449  	communityStruct := otu.GenerateCommunityStruct("account", "dao")
   450  	communityPayload := otu.GenerateCommunityPayload("account", communityStruct)
   451  
   452  	response := otu.CreateCommunityAPI(communityPayload)
   453  
   454  	// Check response code
   455  	checkResponseCode(t, http.StatusCreated, response.Code)
   456  
   457  	// Fetch community to compare updated version against
   458  	response = otu.GetCommunityAPI(1)
   459  
   460  	// Get the original community from the API
   461  	var oldCommunity models.Community
   462  	json.Unmarshal(response.Body.Bytes(), &oldCommunity)
   463  
   464  	// Update some fields
   465  	payload := otu.GenerateCommunityPayload("account", &utils.UpdatedCommunity)
   466  	thresholdOne := "1"
   467  	payload.Proposal_threshold = &thresholdOne
   468  
   469  	response = otu.UpdateCommunityAPI(oldCommunity.ID, payload)
   470  	checkResponseCode(t, http.StatusOK, response.Code)
   471  
   472  	// Get community again for assertions
   473  	response = otu.GetCommunityAPI(oldCommunity.ID)
   474  	var updatedCommunity models.Community
   475  	json.Unmarshal(response.Body.Bytes(), &updatedCommunity)
   476  
   477  	assert.Equal(t, oldCommunity.ID, updatedCommunity.ID)
   478  	assert.Equal(t, *utils.UpdatedCommunity.Logo, *updatedCommunity.Logo)
   479  	assert.Equal(t, *utils.UpdatedCommunity.Strategies, *updatedCommunity.Strategies)
   480  	assert.Equal(t, *utils.UpdatedCommunity.Banner_img_url, *updatedCommunity.Banner_img_url)
   481  	assert.Equal(t, *utils.UpdatedCommunity.Website_url, *updatedCommunity.Website_url)
   482  	assert.Equal(t, *utils.UpdatedCommunity.Twitter_url, *updatedCommunity.Twitter_url)
   483  	assert.Equal(t, *utils.UpdatedCommunity.Github_url, *updatedCommunity.Github_url)
   484  	assert.Equal(t, *utils.UpdatedCommunity.Discord_url, *updatedCommunity.Discord_url)
   485  	assert.Equal(t, *utils.UpdatedCommunity.Instagram_url, *updatedCommunity.Instagram_url)
   486  }