code.gitea.io/gitea@v1.22.3/services/forms/user_form_test.go (about)

     1  // Copyright 2018 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package forms
     5  
     6  import (
     7  	"strconv"
     8  	"testing"
     9  
    10  	auth_model "code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/modules/setting"
    12  
    13  	"github.com/gobwas/glob"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestRegisterForm_IsDomainAllowed_Empty(t *testing.T) {
    18  	oldService := setting.Service
    19  	defer func() {
    20  		setting.Service = oldService
    21  	}()
    22  
    23  	setting.Service.EmailDomainAllowList = nil
    24  
    25  	form := RegisterForm{}
    26  
    27  	assert.True(t, form.IsEmailDomainAllowed())
    28  }
    29  
    30  func TestRegisterForm_IsDomainAllowed_InvalidEmail(t *testing.T) {
    31  	oldService := setting.Service
    32  	defer func() {
    33  		setting.Service = oldService
    34  	}()
    35  
    36  	setting.Service.EmailDomainAllowList = []glob.Glob{glob.MustCompile("gitea.io")}
    37  
    38  	tt := []struct {
    39  		email string
    40  	}{
    41  		{"invalid-email"},
    42  		{"gitea.io"},
    43  	}
    44  
    45  	for _, v := range tt {
    46  		form := RegisterForm{Email: v.email}
    47  
    48  		assert.False(t, form.IsEmailDomainAllowed())
    49  	}
    50  }
    51  
    52  func TestRegisterForm_IsDomainAllowed_AllowedEmail(t *testing.T) {
    53  	oldService := setting.Service
    54  	defer func() {
    55  		setting.Service = oldService
    56  	}()
    57  
    58  	setting.Service.EmailDomainAllowList = []glob.Glob{glob.MustCompile("gitea.io"), glob.MustCompile("*.allow")}
    59  
    60  	tt := []struct {
    61  		email string
    62  		valid bool
    63  	}{
    64  		{"security@gitea.io", true},
    65  		{"security@gITea.io", true},
    66  		{"invalid", false},
    67  		{"seee@example.com", false},
    68  
    69  		{"user@my.allow", true},
    70  		{"user@my.allow1", false},
    71  	}
    72  
    73  	for _, v := range tt {
    74  		form := RegisterForm{Email: v.email}
    75  
    76  		assert.Equal(t, v.valid, form.IsEmailDomainAllowed())
    77  	}
    78  }
    79  
    80  func TestRegisterForm_IsDomainAllowed_BlockedEmail(t *testing.T) {
    81  	oldService := setting.Service
    82  	defer func() {
    83  		setting.Service = oldService
    84  	}()
    85  
    86  	setting.Service.EmailDomainAllowList = nil
    87  	setting.Service.EmailDomainBlockList = []glob.Glob{glob.MustCompile("gitea.io"), glob.MustCompile("*.block")}
    88  
    89  	tt := []struct {
    90  		email string
    91  		valid bool
    92  	}{
    93  		{"security@gitea.io", false},
    94  		{"security@gitea.example", true},
    95  		{"invalid", true},
    96  
    97  		{"user@my.block", false},
    98  		{"user@my.block1", true},
    99  	}
   100  
   101  	for _, v := range tt {
   102  		form := RegisterForm{Email: v.email}
   103  
   104  		assert.Equal(t, v.valid, form.IsEmailDomainAllowed())
   105  	}
   106  }
   107  
   108  func TestNewAccessTokenForm_GetScope(t *testing.T) {
   109  	tests := []struct {
   110  		form        NewAccessTokenForm
   111  		scope       auth_model.AccessTokenScope
   112  		expectedErr error
   113  	}{
   114  		{
   115  			form:  NewAccessTokenForm{Name: "test", Scope: []string{"read:repository"}},
   116  			scope: "read:repository",
   117  		},
   118  		{
   119  			form:  NewAccessTokenForm{Name: "test", Scope: []string{"read:repository", "write:user"}},
   120  			scope: "read:repository,write:user",
   121  		},
   122  	}
   123  
   124  	for i, test := range tests {
   125  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   126  			scope, err := test.form.GetScope()
   127  			assert.Equal(t, test.expectedErr, err)
   128  			assert.Equal(t, test.scope, scope)
   129  		})
   130  	}
   131  }