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

     1  package clerk
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestJWTTemplatesService_ListAll(t *testing.T) {
    14  	c, mux, _, teardown := setup("token")
    15  	defer teardown()
    16  
    17  	dummyResponse := "[" + dummyJWTTemplateJSON + "]"
    18  
    19  	mux.HandleFunc("/jwt_templates", func(w http.ResponseWriter, req *http.Request) {
    20  		testHttpMethod(t, req, http.MethodGet)
    21  		testHeader(t, req, "Authorization", "Bearer token")
    22  		_, _ = fmt.Fprint(w, dummyResponse)
    23  	})
    24  
    25  	got, err := c.JWTTemplates().ListAll()
    26  	assert.Nil(t, err)
    27  
    28  	expected := make([]JWTTemplate, 0)
    29  	_ = json.Unmarshal([]byte(dummyResponse), &expected)
    30  
    31  	if !reflect.DeepEqual(got, expected) {
    32  		t.Errorf("Response = %v, want %v", got, expected)
    33  	}
    34  }
    35  
    36  func TestJWTTemplatesService_Read(t *testing.T) {
    37  	dummyResponse := dummyTemplateJSON
    38  
    39  	c, mux, _, teardown := setup("token")
    40  	defer teardown()
    41  
    42  	url := fmt.Sprintf("/jwt_templates/%s", dummyJWTTemplateID)
    43  
    44  	mux.HandleFunc(url, func(w http.ResponseWriter, req *http.Request) {
    45  		testHttpMethod(t, req, http.MethodGet)
    46  		testHeader(t, req, "Authorization", "Bearer token")
    47  		_, _ = fmt.Fprint(w, dummyResponse)
    48  	})
    49  
    50  	got, err := c.JWTTemplates().Read(dummyJWTTemplateID)
    51  	assert.Nil(t, err)
    52  
    53  	expected := JWTTemplate{}
    54  	_ = json.Unmarshal([]byte(dummyResponse), &expected)
    55  
    56  	if !reflect.DeepEqual(*got, expected) {
    57  		t.Errorf("Response = %v, want %v", got, expected)
    58  	}
    59  }
    60  
    61  func TestJWTTemplatesService_Create(t *testing.T) {
    62  	dummyResponse := dummyJWTTemplateJSON
    63  
    64  	c, mux, _, teardown := setup("token")
    65  	defer teardown()
    66  
    67  	mux.HandleFunc("/jwt_templates", func(w http.ResponseWriter, req *http.Request) {
    68  		testHttpMethod(t, req, http.MethodPost)
    69  		testHeader(t, req, "Authorization", "Bearer token")
    70  		_, _ = fmt.Fprint(w, dummyResponse)
    71  	})
    72  
    73  	newJWTTmpl := &CreateUpdateJWTTemplate{
    74  		Name: "Testing",
    75  		Claims: map[string]interface{}{
    76  			"name": "{{user.first_name}}",
    77  			"role": "tester",
    78  		},
    79  	}
    80  
    81  	got, err := c.JWTTemplates().Create(newJWTTmpl)
    82  	assert.Nil(t, err)
    83  
    84  	expected := JWTTemplate{}
    85  	_ = json.Unmarshal([]byte(dummyResponse), &expected)
    86  
    87  	if !reflect.DeepEqual(*got, expected) {
    88  		t.Errorf("Response = %v, want %v", got, expected)
    89  	}
    90  }
    91  
    92  func TestJWTTemplatesService_CreateWithCustomSigningKey(t *testing.T) {
    93  	c, mux, _, teardown := setup("token")
    94  	defer teardown()
    95  
    96  	customSigningAlgorithm := "HS256"
    97  	customSigningKey := "random-secret"
    98  
    99  	mux.HandleFunc("/jwt_templates", func(w http.ResponseWriter, r *http.Request) {
   100  		testHttpMethod(t, r, http.MethodPost)
   101  		testHeader(t, r, "Authorization", "Bearer token")
   102  
   103  		var req createUpdateJWTTemplateRequest
   104  		_ = json.NewDecoder(r.Body).Decode(&req)
   105  
   106  		assert.Equal(t, true, req.CustomSigningKey)
   107  		assert.Equal(t, customSigningAlgorithm, *req.SigningAlgorithm)
   108  		assert.Equal(t, customSigningKey, *req.SigningKey)
   109  
   110  		_, _ = fmt.Fprint(w, dummyJWTTemplateCustomSigningKeyJSON)
   111  	})
   112  
   113  	newJWTTmpl := &CreateUpdateJWTTemplate{
   114  		Name: "Testing-Custom-Signing-Key",
   115  		Claims: map[string]interface{}{
   116  			"name": "{{user.first_name}}",
   117  			"role": "tester",
   118  		},
   119  		CustomSigningKey: true,
   120  		SigningAlgorithm: &customSigningAlgorithm,
   121  		SigningKey:       &customSigningKey,
   122  	}
   123  
   124  	_, err := c.JWTTemplates().Create(newJWTTmpl)
   125  	assert.Nil(t, err)
   126  }
   127  
   128  func TestJWTTemplatesService_Update(t *testing.T) {
   129  	dummyResponse := dummyJWTTemplateCustomLifetimeAndClockSkewJSON
   130  
   131  	c, mux, _, teardown := setup("token")
   132  	defer teardown()
   133  
   134  	url := fmt.Sprintf("/jwt_templates/%s", dummyJWTTemplateID)
   135  
   136  	mux.HandleFunc(url, func(w http.ResponseWriter, req *http.Request) {
   137  		testHttpMethod(t, req, http.MethodPatch)
   138  		testHeader(t, req, "Authorization", "Bearer token")
   139  		_, _ = fmt.Fprint(w, dummyResponse)
   140  	})
   141  
   142  	updateJWTTmpl := &CreateUpdateJWTTemplate{
   143  		Name: "New-Testing",
   144  		Claims: map[string]interface{}{
   145  			"name": "{{user.first_name}}",
   146  			"age":  28,
   147  		},
   148  	}
   149  
   150  	got, err := c.JWTTemplates().Update(dummyJWTTemplateID, updateJWTTmpl)
   151  	assert.Nil(t, err)
   152  
   153  	expected := JWTTemplate{}
   154  	_ = json.Unmarshal([]byte(dummyResponse), &expected)
   155  
   156  	if !reflect.DeepEqual(*got, expected) {
   157  		t.Errorf("Response = %v, want %v", got, expected)
   158  	}
   159  }
   160  
   161  func TestJWTTemplatesService_Delete(t *testing.T) {
   162  	c, mux, _, teardown := setup("token")
   163  	defer teardown()
   164  
   165  	url := fmt.Sprintf("/jwt_templates/%s", dummyJWTTemplateID)
   166  
   167  	mux.HandleFunc(url, func(w http.ResponseWriter, req *http.Request) {
   168  		testHttpMethod(t, req, http.MethodDelete)
   169  		testHeader(t, req, "Authorization", "Bearer token")
   170  		response := fmt.Sprintf(`{ "deleted": true, "id": "%s", "object": "jwt_template" }`, dummyJWTTemplateID)
   171  		_, _ = fmt.Fprint(w, response)
   172  	})
   173  
   174  	expected := DeleteResponse{
   175  		ID:      dummyJWTTemplateID,
   176  		Object:  "jwt_template",
   177  		Deleted: true,
   178  	}
   179  
   180  	got, err := c.JWTTemplates().Delete(dummyJWTTemplateID)
   181  	assert.Nil(t, err)
   182  
   183  	if !reflect.DeepEqual(*got, expected) {
   184  		t.Errorf("Response = %v, want %v", *got, expected)
   185  	}
   186  }
   187  
   188  const (
   189  	dummyJWTTemplateID = "jtmp_21xC2Ziqscwjq43MtC3CN6Pngbo"
   190  
   191  	dummyJWTTemplateJSON = `
   192  {
   193      "object": "jwt_template",
   194  	"id": "` + dummyJWTTemplateID + `",
   195      "name": "Testing",
   196      "claims": {
   197  		"name": "{{user.first_name}}",
   198  		"role": "tester"
   199  	},
   200  	"lifetime": 60,
   201  	"allowed_clock_skew": 5,
   202  	"custom_signing_key": false,
   203  	"signing_algorithm": "RS256"
   204  }`
   205  
   206  	dummyJWTTemplateCustomLifetimeAndClockSkewJSON = `
   207  {
   208      "object": "jwt_template",
   209  	"id": "` + dummyJWTTemplateID + `",
   210      "name": "New-Testing",
   211      "claims": {
   212  		"name": "{{user.first_name}}",
   213  		"age": 28
   214  	},
   215  	"lifetime": 60,
   216  	"allowed_clock_skew": 5,
   217  	"custom_signing_key": false,
   218  	"signing_algorithm": "RS256"
   219  }`
   220  
   221  	dummyJWTTemplateCustomSigningKeyJSON = `
   222  {
   223      "object": "jwt_template",
   224  	"id": "` + dummyJWTTemplateID + `",
   225      "name": "Testing",
   226      "claims": {
   227  		"name": "{{user.first_name}}",
   228  		"role": "tester"
   229  	},
   230  	"lifetime": 60,
   231  	"allowed_clock_skew": 5,
   232  	"custom_signing_key": true,
   233  	"signing_algorithm": "HS256"
   234  }`
   235  )