code.gitea.io/gitea@v1.19.3/modules/hcaptcha/hcaptcha_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package hcaptcha
     5  
     6  import (
     7  	"net/http"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  const (
    15  	dummySiteKey = "10000000-ffff-ffff-ffff-000000000001"
    16  	dummySecret  = "0x0000000000000000000000000000000000000000"
    17  	dummyToken   = "10000000-aaaa-bbbb-cccc-000000000001"
    18  )
    19  
    20  func TestMain(m *testing.M) {
    21  	os.Exit(m.Run())
    22  }
    23  
    24  func TestCaptcha(t *testing.T) {
    25  	tt := []struct {
    26  		Name   string
    27  		Secret string
    28  		Token  string
    29  		Error  ErrorCode
    30  	}{
    31  		{
    32  			Name:   "Success",
    33  			Secret: dummySecret,
    34  			Token:  dummyToken,
    35  		},
    36  		{
    37  			Name:  "Missing Secret",
    38  			Token: dummyToken,
    39  			Error: ErrMissingInputSecret,
    40  		},
    41  		{
    42  			Name:   "Missing Token",
    43  			Secret: dummySecret,
    44  			Error:  ErrMissingInputResponse,
    45  		},
    46  		{
    47  			Name:   "Invalid Token",
    48  			Secret: dummySecret,
    49  			Token:  "test",
    50  			Error:  ErrInvalidInputResponse,
    51  		},
    52  	}
    53  
    54  	for _, tc := range tt {
    55  		t.Run(tc.Name, func(t *testing.T) {
    56  			client, err := New(tc.Secret, WithHTTP(&http.Client{
    57  				Timeout: time.Second * 5,
    58  			}))
    59  			if err != nil {
    60  				// The only error that can be returned from creating a client
    61  				if tc.Error == ErrMissingInputSecret && err == ErrMissingInputSecret {
    62  					return
    63  				}
    64  				t.Log(err)
    65  				t.FailNow()
    66  			}
    67  
    68  			resp, err := client.Verify(tc.Token, PostOptions{
    69  				Sitekey: dummySiteKey,
    70  			})
    71  			if err != nil {
    72  				// The only error that can be returned prior to the request
    73  				if tc.Error == ErrMissingInputResponse && err == ErrMissingInputResponse {
    74  					return
    75  				}
    76  				t.Log(err)
    77  				t.FailNow()
    78  			}
    79  
    80  			if tc.Error.String() != "" {
    81  				if resp.Success {
    82  					t.Log("Verification should fail.")
    83  					t.Fail()
    84  				}
    85  				if len(resp.ErrorCodes) == 0 {
    86  					t.Log("hCaptcha should have returned an error.")
    87  					t.Fail()
    88  				}
    89  				var hasErr bool
    90  				for _, err := range resp.ErrorCodes {
    91  					if strings.EqualFold(err.String(), tc.Error.String()) {
    92  						hasErr = true
    93  						break
    94  					}
    95  				}
    96  				if !hasErr {
    97  					t.Log("hCaptcha did not return the error being tested")
    98  					t.Fail()
    99  				}
   100  			} else if !resp.Success {
   101  				t.Log("Verification should succeed.")
   102  				t.Fail()
   103  			}
   104  		})
   105  	}
   106  }