github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/test/login_test.go (about)

     1  //go:build kind
     2  // +build kind
     3  
     4  package test
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  // TestCorruptedTokenLogin checks that if we corrupt the token we successfully reset the config and clear the token
    14  // to allow the user to login again rather than leave them in a state of limbo where they have to munge the config
    15  // themselves
    16  func TestCorruptedTokenLogin(t *testing.T) {
    17  	// @todo this test is disabled now because it was
    18  	// built on internal assumptions that no longer hold and not so easy to access anymore
    19  	// TrySuite(t, testCorruptedLogin, retryCount)
    20  }
    21  
    22  func testCorruptedLogin(t *T) {
    23  	serv := NewServer(t)
    24  	defer serv.Close()
    25  	if err := serv.Run(); err != nil {
    26  		return
    27  	}
    28  
    29  	t.Parallel()
    30  
    31  	// get server command
    32  	cmd := serv.Command()
    33  
    34  	outp, _ := cmd.Exec("status")
    35  	unauthorizedMessage := "account is required"
    36  	if !strings.Contains(string(outp), unauthorizedMessage) {
    37  		t.Fatalf("Call should need authorization")
    38  	}
    39  	outp, _ = cmd.Exec("login", "--email", "admin", "--password", "micro")
    40  	if !strings.Contains(string(outp), "Successfully logged in.") {
    41  		t.Fatalf("Login failed: %s", outp)
    42  	}
    43  	outp, _ = cmd.Exec("status")
    44  	if string(outp) != "" {
    45  		t.Fatalf("Call should receive no output: %s", outp)
    46  	}
    47  	// munge token
    48  	tok, err := cmd.Exec("user", "config", "get", "micro.auth."+serv.Env()+".refresh-token")
    49  	if err != nil {
    50  		t.Fatalf("Error getting refresh token value %s", err)
    51  	}
    52  	if _, err := cmd.Exec("user", "config", "set", "micro.auth."+serv.Env()+".refresh-token", strings.TrimSpace(string(tok))+"a"); err != nil {
    53  		t.Fatalf("Error setting refresh token value %s", err)
    54  	}
    55  	if _, err := cmd.Exec("user", "config", "set", "micro.auth."+serv.Env()+".expiry", fmt.Sprintf("%d", time.Now().Add(-1*time.Hour).Unix())); err != nil {
    56  		t.Fatalf("Error getting refresh token expiry %s", err)
    57  	}
    58  
    59  	outp, _ = cmd.Exec("status")
    60  	if !strings.Contains(string(outp), unauthorizedMessage) {
    61  		t.Fatalf("Call should have failed: %s", outp)
    62  	}
    63  	outp, _ = cmd.Exec("login", "--email", "admin", "--password", "micro")
    64  	if !strings.Contains(string(outp), "Successfully logged in.") {
    65  		t.Fatalf("Login failed: %s", outp)
    66  	}
    67  
    68  }