Added Email of Keystone to Identity (#1681)
* Added Email of Keystone to Identity After the successful login to keystone, the Email of the logged in user is fetch from keystone and provided to `identity.Email`. This is useful for upstream software that uses the Email as the primary identification. * Removed unnecessary code from getUsers * Changed creation of userResponse in keystone * Fixing linter error Co-authored-by: Christoph Glaubitz <christoph.glaubitz@innovo-cloud.de>
This commit is contained in:
@@ -35,12 +35,6 @@ var (
|
||||
groupsURL = ""
|
||||
)
|
||||
|
||||
type userResponse struct {
|
||||
User struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"user"`
|
||||
}
|
||||
|
||||
type groupResponse struct {
|
||||
Group struct {
|
||||
ID string `json:"id"`
|
||||
@@ -144,7 +138,7 @@ func createUser(t *testing.T, token, userName, userEmail, userPass string) strin
|
||||
}
|
||||
|
||||
// delete group or user
|
||||
func delete(t *testing.T, token, id, uri string) {
|
||||
func deleteResource(t *testing.T, token, id, uri string) {
|
||||
t.Helper()
|
||||
client := &http.Client{}
|
||||
|
||||
@@ -246,20 +240,86 @@ func TestIncorrectCredentialsLogin(t *testing.T) {
|
||||
func TestValidUserLogin(t *testing.T) {
|
||||
setupVariables(t)
|
||||
token, _ := getAdminToken(t, adminUser, adminPass)
|
||||
userID := createUser(t, token, testUser, testEmail, testPass)
|
||||
c := conn{Host: keystoneURL, Domain: testDomain,
|
||||
AdminUsername: adminUser, AdminPassword: adminPass}
|
||||
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
||||
identity, validPW, err := c.Login(context.Background(), s, testUser, testPass)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
t.Log(identity)
|
||||
|
||||
if !validPW {
|
||||
t.Fatal("Valid password was not accepted")
|
||||
type tUser struct {
|
||||
username string
|
||||
domain string
|
||||
email string
|
||||
password string
|
||||
}
|
||||
|
||||
type expect struct {
|
||||
username string
|
||||
email string
|
||||
verifiedEmail bool
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
input tUser
|
||||
expected expect
|
||||
}{
|
||||
{
|
||||
name: "test with email address",
|
||||
input: tUser{
|
||||
username: testUser,
|
||||
domain: testDomain,
|
||||
email: testEmail,
|
||||
password: testPass,
|
||||
},
|
||||
expected: expect{
|
||||
username: testUser,
|
||||
email: testEmail,
|
||||
verifiedEmail: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "test without email address",
|
||||
input: tUser{
|
||||
username: testUser,
|
||||
domain: testDomain,
|
||||
email: "",
|
||||
password: testPass,
|
||||
},
|
||||
expected: expect{
|
||||
username: testUser,
|
||||
email: "",
|
||||
verifiedEmail: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
userID := createUser(t, token, tt.input.username, tt.input.email, tt.input.password)
|
||||
defer deleteResource(t, token, userID, usersURL)
|
||||
|
||||
c := conn{Host: keystoneURL, Domain: tt.input.domain,
|
||||
AdminUsername: adminUser, AdminPassword: adminPass}
|
||||
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
||||
identity, validPW, err := c.Login(context.Background(), s, tt.input.username, tt.input.password)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
t.Log(identity)
|
||||
if identity.Username != tt.expected.username {
|
||||
t.Fatalf("Invalid user. Got: %v. Wanted: %v", identity.Username, tt.expected.username)
|
||||
}
|
||||
if identity.UserID == "" {
|
||||
t.Fatalf("Didn't get any UserID back")
|
||||
}
|
||||
if identity.Email != tt.expected.email {
|
||||
t.Fatalf("Invalid email. Got: %v. Wanted: %v", identity.Email, tt.expected.email)
|
||||
}
|
||||
if identity.EmailVerified != tt.expected.verifiedEmail {
|
||||
t.Fatalf("Invalid verifiedEmail. Got: %v. Wanted: %v", identity.EmailVerified, tt.expected.verifiedEmail)
|
||||
}
|
||||
|
||||
if !validPW {
|
||||
t.Fatal("Valid password was not accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
delete(t, token, userID, usersURL)
|
||||
}
|
||||
|
||||
func TestUseRefreshToken(t *testing.T) {
|
||||
@@ -267,6 +327,7 @@ func TestUseRefreshToken(t *testing.T) {
|
||||
token, adminID := getAdminToken(t, adminUser, adminPass)
|
||||
groupID := createGroup(t, token, "Test group description", testGroup)
|
||||
addUserToGroup(t, token, groupID, adminID)
|
||||
defer deleteResource(t, token, groupID, groupsURL)
|
||||
|
||||
c := conn{Host: keystoneURL, Domain: testDomain,
|
||||
AdminUsername: adminUser, AdminPassword: adminPass}
|
||||
@@ -282,8 +343,6 @@ func TestUseRefreshToken(t *testing.T) {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
delete(t, token, groupID, groupsURL)
|
||||
|
||||
expectEquals(t, 1, len(identityRefresh.Groups))
|
||||
expectEquals(t, testGroup, identityRefresh.Groups[0])
|
||||
}
|
||||
@@ -307,7 +366,7 @@ func TestUseRefreshTokenUserDeleted(t *testing.T) {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
delete(t, token, userID, usersURL)
|
||||
deleteResource(t, token, userID, usersURL)
|
||||
_, err = c.Refresh(context.Background(), s, identityLogin)
|
||||
|
||||
if !strings.Contains(err.Error(), "does not exist") {
|
||||
@@ -319,6 +378,7 @@ func TestUseRefreshTokenGroupsChanged(t *testing.T) {
|
||||
setupVariables(t)
|
||||
token, _ := getAdminToken(t, adminUser, adminPass)
|
||||
userID := createUser(t, token, testUser, testEmail, testPass)
|
||||
defer deleteResource(t, token, userID, usersURL)
|
||||
|
||||
c := conn{Host: keystoneURL, Domain: testDomain,
|
||||
AdminUsername: adminUser, AdminPassword: adminPass}
|
||||
@@ -338,15 +398,13 @@ func TestUseRefreshTokenGroupsChanged(t *testing.T) {
|
||||
|
||||
groupID := createGroup(t, token, "Test group", testGroup)
|
||||
addUserToGroup(t, token, groupID, userID)
|
||||
defer deleteResource(t, token, groupID, groupsURL)
|
||||
|
||||
identityRefresh, err = c.Refresh(context.Background(), s, identityLogin)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
delete(t, token, groupID, groupsURL)
|
||||
delete(t, token, userID, usersURL)
|
||||
|
||||
expectEquals(t, 1, len(identityRefresh.Groups))
|
||||
}
|
||||
|
||||
@@ -354,6 +412,7 @@ func TestNoGroupsInScope(t *testing.T) {
|
||||
setupVariables(t)
|
||||
token, _ := getAdminToken(t, adminUser, adminPass)
|
||||
userID := createUser(t, token, testUser, testEmail, testPass)
|
||||
defer deleteResource(t, token, userID, usersURL)
|
||||
|
||||
c := conn{Host: keystoneURL, Domain: testDomain,
|
||||
AdminUsername: adminUser, AdminPassword: adminPass}
|
||||
@@ -361,6 +420,7 @@ func TestNoGroupsInScope(t *testing.T) {
|
||||
|
||||
groupID := createGroup(t, token, "Test group", testGroup)
|
||||
addUserToGroup(t, token, groupID, userID)
|
||||
defer deleteResource(t, token, groupID, groupsURL)
|
||||
|
||||
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
|
||||
if err != nil {
|
||||
@@ -373,9 +433,6 @@ func TestNoGroupsInScope(t *testing.T) {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
expectEquals(t, 0, len(identityRefresh.Groups))
|
||||
|
||||
delete(t, token, groupID, groupsURL)
|
||||
delete(t, token, userID, usersURL)
|
||||
}
|
||||
|
||||
func setupVariables(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user