From 78a02529abc3f9ebfdcfbb73164c494f8143b629 Mon Sep 17 00:00:00 2001 From: Christian Claus Date: Wed, 23 May 2018 21:40:43 +0200 Subject: [PATCH] Add authentication test --- app/security_test.go | 108 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 app/security_test.go diff --git a/app/security_test.go b/app/security_test.go new file mode 100644 index 0000000..c21863b --- /dev/null +++ b/app/security_test.go @@ -0,0 +1,108 @@ +package app + +import ( + "reflect" + "testing" +) + +func TestAuthenticate(t *testing.T) { + type args struct { + config *Config + username string + password string + } + tests := []struct { + name string + args args + want *AuthInfo + wantErr bool + }{ + { + "empty username", + args{ + config: &Config{}, + username: "", + password: "password", + }, + &AuthInfo{ + Username: "", + Authenticated: false, + }, + true, + }, + { + "empty password", + args{ + config: &Config{}, + username: "foo", + password: "", + }, + &AuthInfo{ + Username: "foo", + Authenticated: false, + }, + true, + }, + { + "user not found", + args{ + config: &Config{Users: map[string]*UserInfo{ + "bar": nil, + }}, + username: "foo", + password: "password", + }, + &AuthInfo{ + Username: "foo", + Authenticated: false, + }, + true, + }, + { + "password doesn't match", + args{ + config: &Config{Users: map[string]*UserInfo{ + "foo": { + Password: GenHash([]byte("not-my-password")), + }, + }}, + username: "foo", + password: "password", + }, + &AuthInfo{ + Username: "foo", + Authenticated: false, + }, + true, + }, + { + "all fine", + args{ + config: &Config{Users: map[string]*UserInfo{ + "foo": { + Password: GenHash([]byte("password")), + }, + }}, + username: "foo", + password: "password", + }, + &AuthInfo{ + Username: "foo", + Authenticated: true, + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := authenticate(tt.args.config, tt.args.username, tt.args.password) + if (err != nil) != tt.wantErr { + t.Errorf("authenticate() name = %v, error = %v, wantErr %v", tt.name, err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("authenticate() name = %v, got = %v, want %v", tt.name, got, tt.want) + } + }) + } +}