gitlab.com/SiaPrime/SiaPrime@v1.4.1/node/api/server_test.go (about)

     1  package api
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  )
     7  
     8  // TestExplorerPreset checks that the default configuration for the explorer is
     9  // working correctly.
    10  func TestExplorerPreset(t *testing.T) {
    11  	if testing.Short() {
    12  		t.SkipNow()
    13  	}
    14  	t.Parallel()
    15  	st, err := createExplorerServerTester(t.Name())
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer st.server.panicClose()
    20  
    21  	// Try calling a legal endpoint without a user agent.
    22  	err = st.stdGetAPIUA("/explorer", "")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  }
    27  
    28  // TestReloading reloads a server and does smoke testing to see that modules
    29  // are still working after reload.
    30  func TestReloading(t *testing.T) {
    31  	if testing.Short() {
    32  		t.SkipNow()
    33  	}
    34  	t.Parallel()
    35  	st, err := createServerTester(t.Name())
    36  
    37  	height := st.server.api.cs.Height()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	err = st.server.Close()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	rst, err := st.reloadedServerTester()
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	defer rst.server.panicClose()
    50  	if height != rst.server.api.cs.Height() {
    51  		t.Error("server heights do not match")
    52  	}
    53  
    54  	// Mine some blocks on the reloaded server and see if any errors or panics
    55  	// are triggered.
    56  	for i := 0; i < 3; i++ {
    57  		_, err := rst.miner.AddBlock()
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  	}
    62  }
    63  
    64  // TestAuthenticated tests creating a server that requires authenticated API
    65  // calls, and then makes (un)authenticated API calls to test the
    66  // authentication.
    67  func TestAuthentication(t *testing.T) {
    68  	if testing.Short() {
    69  		t.SkipNow()
    70  	}
    71  	t.Parallel()
    72  	st, err := createAuthenticatedServerTester(t.Name(), "password", false)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer st.server.panicClose()
    77  
    78  	testGETURL := "http://" + st.server.listener.Addr().String() + "/wallet/seeds"
    79  	testPOSTURL := "http://" + st.server.listener.Addr().String() + "/host/announce"
    80  
    81  	// Test that unauthenticated API calls fail.
    82  	// GET
    83  	resp, err := HttpGET(testGETURL)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	if resp.StatusCode != http.StatusUnauthorized {
    88  		t.Fatal("unauthenticated API call succeeded on a server that requires authentication")
    89  	}
    90  	// POST
    91  	resp, err = HttpPOST(testPOSTURL, "")
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	if resp.StatusCode != http.StatusUnauthorized {
    96  		t.Fatal("unauthenticated API call succeeded on a server that requires authentication")
    97  	}
    98  
    99  	// Test that authenticated API calls with the wrong password fail.
   100  	// GET
   101  	resp, err = HttpGETAuthenticated(testGETURL, "wrong password")
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	if resp.StatusCode != http.StatusUnauthorized {
   106  		t.Fatal("authenticated API call succeeded with an incorrect password")
   107  	}
   108  	// POST
   109  	resp, err = HttpPOSTAuthenticated(testPOSTURL, "", "wrong password")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	if resp.StatusCode != http.StatusUnauthorized {
   114  		t.Fatal("authenticated API call succeeded with an incorrect password")
   115  	}
   116  
   117  	// Test that authenticated API calls with the correct password succeed.
   118  	// GET
   119  	resp, err = HttpGETAuthenticated(testGETURL, "password")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if non2xx(resp.StatusCode) {
   124  		t.Fatal("authenticated API call failed with the correct password")
   125  	}
   126  	// POST
   127  	resp, err = HttpPOSTAuthenticated(testPOSTURL, "", "password")
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	if non2xx(resp.StatusCode) {
   132  		t.Fatal("authenticated API call failed with the correct password")
   133  	}
   134  }