Skip to content
Snippets Groups Projects
auth_test.go 1.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • package server
    
    import (
    	"context"
    	"testing"
    
    	apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
    )
    
    
    func TestAuth_Login(t *testing.T) {
    
    	type args struct {
    		ctx     context.Context
    		request *apb.LoginRequest
    	}
    	tests := []struct {
    		name    string
    		args    args
    		want    string
    		wantErr bool
    	}{
    		{
    
    			name: "default login",
    			want: "testAdmin",
    			args: args{
    				request: &apb.LoginRequest{
    					Username: "testAdmin",
    					Pwd:      "admin",
    				},
    			},
    			wantErr: false,
    		},
    		{
    			name: "login fail wrong pwd",
    
    				request: &apb.LoginRequest{
    					Username: "testAdmin",
    					Pwd:      "nope",
    				},
    
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    
    				jwtManager: jwt,
    
    			}
    			resp, err := r.Login(tt.args.ctx, tt.args.request)
    			if (err != nil) != tt.wantErr {
    
    				t.Errorf("Auth.Login() error = %v, wantErr %v", err, tt.wantErr)
    
    			if resp != nil {
    				got := resp.Token
    				if got == "" {
    					t.Errorf("Auth.Login() = %v, want non empty token", got)
    				}
    
    func TestAuth_Logout(t *testing.T) {
    
    	type args struct {
    		ctx     context.Context
    		request *apb.LogoutRequest
    	}
    	tests := []struct {
    		name    string
    		args    args
    		want    *apb.LogoutResponse
    		wantErr bool
    	}{
    
    			name: "default log out",
    
    			args: args{
    				ctx: context.TODO(),
    
    				request: &apb.LogoutRequest{
    					Username: "testAdmin",
    
    			want: &apb.LogoutResponse{
    
    				Status: apb.Status_STATUS_OK,
    			},
    			wantErr: false,
    		},
    	}
    
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			s := Auth{}
    
    			got, err := s.Logout(tt.args.ctx, tt.args.request)
    
    			if (err != nil) != tt.wantErr {
    
    				t.Errorf("Auth.Logout() error = %v, wantErr %v", err, tt.wantErr)
    
    				return
    			}
    
    			if got != nil && got.Status != tt.want.Status {
    
    				t.Errorf("Role.CreateRoles() = %v, want %v", got, tt.want)