github.com/clerkinc/clerk-sdk-go@v1.49.1/clerk/blocklists_test.go (about)

     1  package clerk
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestBlocklistService_CreateIdentifier_happyPath(t *testing.T) {
    13  	token := "token"
    14  	var blocklistIdentifier BlocklistIdentifierResponse
    15  	_ = json.Unmarshal([]byte(dummyBlocklistIdentifierJson), &blocklistIdentifier)
    16  
    17  	client, mux, _, teardown := setup(token)
    18  	defer teardown()
    19  
    20  	mux.HandleFunc("/blocklist_identifiers", func(w http.ResponseWriter, req *http.Request) {
    21  		testHttpMethod(t, req, http.MethodPost)
    22  		testHeader(t, req, "Authorization", "Bearer "+token)
    23  		fmt.Fprint(w, dummyBlocklistIdentifierJson)
    24  	})
    25  
    26  	got, _ := client.Blocklists().CreateIdentifier(CreateBlocklistIdentifierParams{
    27  		Identifier: blocklistIdentifier.Identifier,
    28  	})
    29  
    30  	assert.Equal(t, &blocklistIdentifier, got)
    31  }
    32  
    33  func TestBlocklistService_CreateIdentifier_invalidServer(t *testing.T) {
    34  	client, _ := NewClient("token")
    35  
    36  	_, err := client.Blocklists().CreateIdentifier(CreateBlocklistIdentifierParams{
    37  		Identifier: "dummy@example.com",
    38  	})
    39  	if err == nil {
    40  		t.Errorf("Expected error to be returned")
    41  	}
    42  }
    43  
    44  func TestBlocklistService_DeleteIdentifier_happyPath(t *testing.T) {
    45  	token := "token"
    46  	var blocklistIdentifier BlocklistIdentifierResponse
    47  	_ = json.Unmarshal([]byte(dummyBlocklistIdentifierJson), &blocklistIdentifier)
    48  
    49  	client, mux, _, teardown := setup(token)
    50  	defer teardown()
    51  
    52  	mux.HandleFunc("/blocklist_identifiers/"+blocklistIdentifier.ID, func(w http.ResponseWriter, req *http.Request) {
    53  		testHttpMethod(t, req, http.MethodDelete)
    54  		testHeader(t, req, "Authorization", "Bearer "+token)
    55  		response := fmt.Sprintf(`{ "deleted": true, "id": "%s", "object": "blocklist_identifier" }`, blocklistIdentifier.ID)
    56  		_, _ = fmt.Fprint(w, response)
    57  	})
    58  
    59  	got, _ := client.Blocklists().DeleteIdentifier(blocklistIdentifier.ID)
    60  	assert.Equal(t, blocklistIdentifier.ID, got.ID)
    61  	assert.True(t, got.Deleted)
    62  }
    63  
    64  func TestBlocklistService_DeleteIdentifier_invalidServer(t *testing.T) {
    65  	client, _ := NewClient("token")
    66  
    67  	_, err := client.Blocklists().DeleteIdentifier("blid_1mvFol71HiKCcypBd6xxg0IpMBN")
    68  	if err == nil {
    69  		t.Errorf("Expected error to be returned")
    70  	}
    71  }
    72  
    73  func TestBlocklistService_ListAllIdentifiers_happyPath(t *testing.T) {
    74  	token := "token"
    75  	var blocklistIdentifier BlocklistIdentifierResponse
    76  	_ = json.Unmarshal([]byte(dummyBlocklistIdentifierJson), &blocklistIdentifier)
    77  
    78  	client, mux, _, teardown := setup(token)
    79  	defer teardown()
    80  
    81  	mux.HandleFunc("/blocklist_identifiers", func(w http.ResponseWriter, req *http.Request) {
    82  		testHttpMethod(t, req, http.MethodGet)
    83  		testHeader(t, req, "Authorization", "Bearer "+token)
    84  		fmt.Fprint(w, fmt.Sprintf(`{"data": [%s], "total_count": 1}`, dummyBlocklistIdentifierJson))
    85  	})
    86  
    87  	got, _ := client.Blocklists().ListAllIdentifiers()
    88  
    89  	assert.Len(t, got.Data, 1)
    90  	assert.Equal(t, int64(1), got.TotalCount)
    91  	assert.Equal(t, &blocklistIdentifier, got.Data[0])
    92  }
    93  
    94  func TestBlocklistService_ListAllIdentifiers_invalidServer(t *testing.T) {
    95  	client, _ := NewClient("token")
    96  
    97  	_, err := client.Blocklists().ListAllIdentifiers()
    98  	if err == nil {
    99  		t.Errorf("Expected error to be returned")
   100  	}
   101  }
   102  
   103  const dummyBlocklistIdentifierJson = `{
   104      "id": "blid_1mvFol71HiKCcypBd6xxg0IpMBN",
   105      "object": "blocklist_identifier",
   106  	"identifier": "dummy@example.com",
   107  	"identifier_type": "email_address",
   108  	"created_at": 1610783813,
   109  	"updated_at": 1610783813
   110  }`