|  | 
|  | 1 | +package user_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"encoding/json" | 
|  | 5 | +	"testing" | 
|  | 6 | + | 
|  | 7 | +	"github.com/stretchr/testify/assert" | 
|  | 8 | +	"github.com/yaoapp/yao/openapi/user" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +// TestConfigTypes tests that our new configuration types are properly defined | 
|  | 12 | +func TestConfigTypes(t *testing.T) { | 
|  | 13 | +	// Test TeamConfig type | 
|  | 14 | +	teamConfig := &user.TeamConfig{ | 
|  | 15 | +		Roles: []*user.TeamRole{ | 
|  | 16 | +			{ | 
|  | 17 | +				RoleID:      "team_owner", | 
|  | 18 | +				Label:       "Owner", | 
|  | 19 | +				Description: "Full access to team settings", | 
|  | 20 | +			}, | 
|  | 21 | +		}, | 
|  | 22 | +		Invite: &user.InviteConfig{ | 
|  | 23 | +			Channel: "default", | 
|  | 24 | +			Expiry:  "1d", | 
|  | 25 | +			Templates: map[string]string{ | 
|  | 26 | +				"mail": "en.invite_message", | 
|  | 27 | +				"sms":  "en.invite_message", | 
|  | 28 | +			}, | 
|  | 29 | +		}, | 
|  | 30 | +	} | 
|  | 31 | + | 
|  | 32 | +	assert.NotNil(t, teamConfig, "TeamConfig should not be nil") | 
|  | 33 | +	assert.Len(t, teamConfig.Roles, 1, "Should have 1 role") | 
|  | 34 | +	assert.Equal(t, "team_owner", teamConfig.Roles[0].RoleID, "Role ID should match") | 
|  | 35 | +	assert.Equal(t, "Owner", teamConfig.Roles[0].Label, "Role label should match") | 
|  | 36 | +	assert.NotNil(t, teamConfig.Invite, "Invite config should not be nil") | 
|  | 37 | +	assert.Equal(t, "default", teamConfig.Invite.Channel, "Channel should match") | 
|  | 38 | +	assert.Equal(t, "1d", teamConfig.Invite.Expiry, "Expiry should match") | 
|  | 39 | +	assert.Len(t, teamConfig.Invite.Templates, 2, "Should have 2 templates") | 
|  | 40 | + | 
|  | 41 | +	// Test TeamRole type | 
|  | 42 | +	role := &user.TeamRole{ | 
|  | 43 | +		RoleID:      "team_admin", | 
|  | 44 | +		Label:       "Admin", | 
|  | 45 | +		Description: "Manage team members", | 
|  | 46 | +	} | 
|  | 47 | + | 
|  | 48 | +	assert.Equal(t, "team_admin", role.RoleID, "Role ID should match") | 
|  | 49 | +	assert.Equal(t, "Admin", role.Label, "Role label should match") | 
|  | 50 | +	assert.Equal(t, "Manage team members", role.Description, "Description should match") | 
|  | 51 | + | 
|  | 52 | +	// Test InviteConfig type | 
|  | 53 | +	inviteConfig := &user.InviteConfig{ | 
|  | 54 | +		Channel: "email", | 
|  | 55 | +		Expiry:  "24h", | 
|  | 56 | +		Templates: map[string]string{ | 
|  | 57 | +			"mail": "zh-cn.invite_message", | 
|  | 58 | +		}, | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	assert.Equal(t, "email", inviteConfig.Channel, "Channel should match") | 
|  | 62 | +	assert.Equal(t, "24h", inviteConfig.Expiry, "Expiry should match") | 
|  | 63 | +	assert.Len(t, inviteConfig.Templates, 1, "Should have 1 template") | 
|  | 64 | +	assert.Equal(t, "zh-cn.invite_message", inviteConfig.Templates["mail"], "Template should match") | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +// TestConfigTypeCompatibility tests that our types are compatible with JSON marshaling | 
|  | 68 | +func TestConfigTypeCompatibility(t *testing.T) { | 
|  | 69 | +	// Test TeamConfig JSON marshaling | 
|  | 70 | +	teamConfig := &user.TeamConfig{ | 
|  | 71 | +		Roles: []*user.TeamRole{ | 
|  | 72 | +			{ | 
|  | 73 | +				RoleID:      "test_role", | 
|  | 74 | +				Label:       "Test Role", | 
|  | 75 | +				Description: "A test role", | 
|  | 76 | +			}, | 
|  | 77 | +		}, | 
|  | 78 | +		Invite: &user.InviteConfig{ | 
|  | 79 | +			Channel: "test", | 
|  | 80 | +			Expiry:  "1h", | 
|  | 81 | +			Templates: map[string]string{ | 
|  | 82 | +				"test": "test_template", | 
|  | 83 | +			}, | 
|  | 84 | +		}, | 
|  | 85 | +	} | 
|  | 86 | + | 
|  | 87 | +	// Test that the struct can be marshaled to JSON using standard library | 
|  | 88 | +	jsonData, err := json.Marshal(teamConfig) | 
|  | 89 | +	assert.NoError(t, err, "Should marshal to JSON without error") | 
|  | 90 | +	assert.NotEmpty(t, jsonData, "JSON data should not be empty") | 
|  | 91 | + | 
|  | 92 | +	// Test that the struct can be unmarshaled from JSON | 
|  | 93 | +	var unmarshaledConfig user.TeamConfig | 
|  | 94 | +	err = json.Unmarshal(jsonData, &unmarshaledConfig) | 
|  | 95 | +	assert.NoError(t, err, "Should unmarshal from JSON without error") | 
|  | 96 | +	assert.Equal(t, teamConfig.Roles[0].RoleID, unmarshaledConfig.Roles[0].RoleID, "Role ID should match after unmarshaling") | 
|  | 97 | +	assert.Equal(t, teamConfig.Invite.Channel, unmarshaledConfig.Invite.Channel, "Channel should match after unmarshaling") | 
|  | 98 | +} | 
0 commit comments