-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move validation of pre auth key out of db
This move separates the logic a bit and allow us to write specific errors for the caller, in this case the web layer so we can present the user with the correct error codes without bleeding web stuff into a generic validate. Signed-off-by: Kristoffer Dalby <[email protected]>
- Loading branch information
Showing
5 changed files
with
186 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package hscontrol | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/juanfont/headscale/hscontrol/types" | ||
) | ||
|
||
func TestCanUsePreAuthKey(t *testing.T) { | ||
now := time.Now() | ||
past := now.Add(-time.Hour) | ||
future := now.Add(time.Hour) | ||
|
||
tests := []struct { | ||
name string | ||
pak *types.PreAuthKey | ||
wantErr bool | ||
err HTTPError | ||
}{ | ||
{ | ||
name: "valid reusable key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: true, | ||
Used: false, | ||
Expiration: &future, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid non-reusable key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: false, | ||
Expiration: &future, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "expired key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: false, | ||
Expiration: &past, | ||
}, | ||
wantErr: true, | ||
err: NewHTTPError(http.StatusUnauthorized, "authkey expired", nil), | ||
}, | ||
{ | ||
name: "used non-reusable key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: true, | ||
Expiration: &future, | ||
}, | ||
wantErr: true, | ||
err: NewHTTPError(http.StatusUnauthorized, "authkey already used", nil), | ||
}, | ||
{ | ||
name: "used reusable key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: true, | ||
Used: true, | ||
Expiration: &future, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "no expiration date", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: false, | ||
Expiration: nil, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "nil preauth key", | ||
pak: nil, | ||
wantErr: true, | ||
err: NewHTTPError(http.StatusUnauthorized, "invalid authkey", nil), | ||
}, | ||
{ | ||
name: "expired and used key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: true, | ||
Expiration: &past, | ||
}, | ||
wantErr: true, | ||
err: NewHTTPError(http.StatusUnauthorized, "authkey expired", nil), | ||
}, | ||
{ | ||
name: "no expiration and used key", | ||
pak: &types.PreAuthKey{ | ||
Reusable: false, | ||
Used: true, | ||
Expiration: nil, | ||
}, | ||
wantErr: true, | ||
err: NewHTTPError(http.StatusUnauthorized, "authkey already used", nil), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := canUsePreAuthKey(tt.pak) | ||
if tt.wantErr { | ||
if err == nil { | ||
t.Errorf("expected error but got none") | ||
} else { | ||
httpErr, ok := err.(HTTPError) | ||
if !ok { | ||
t.Errorf("expected HTTPError but got %T", err) | ||
} else { | ||
if diff := cmp.Diff(tt.err, httpErr); diff != "" { | ||
t.Errorf("unexpected error (-want +got):\n%s", diff) | ||
} | ||
} | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected no error but got %v", err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.