github.com/amar224/phishing-tool@v0.9.0/models/user_test.go (about)

     1  package models
     2  
     3  import (
     4  	"github.com/jinzhu/gorm"
     5  	"gopkg.in/check.v1"
     6  )
     7  
     8  func (s *ModelsSuite) TestGetUserExists(c *check.C) {
     9  	u, err := GetUser(1)
    10  	c.Assert(err, check.Equals, nil)
    11  	c.Assert(u.Username, check.Equals, "admin")
    12  }
    13  
    14  func (s *ModelsSuite) TestGetUserByUsernameWithExistingUser(c *check.C) {
    15  	u, err := GetUserByUsername("admin")
    16  	c.Assert(err, check.Equals, nil)
    17  	c.Assert(u.Username, check.Equals, "admin")
    18  }
    19  
    20  func (s *ModelsSuite) TestGetUserDoesNotExist(c *check.C) {
    21  	u, err := GetUser(100)
    22  	c.Assert(err, check.Equals, gorm.ErrRecordNotFound)
    23  	c.Assert(u.Username, check.Equals, "")
    24  }
    25  
    26  func (s *ModelsSuite) TestGetUserByAPIKeyWithExistingAPIKey(c *check.C) {
    27  	u, err := GetUser(1)
    28  	c.Assert(err, check.Equals, nil)
    29  
    30  	u, err = GetUserByAPIKey(u.ApiKey)
    31  }
    32  
    33  func (s *ModelsSuite) TestGetUserByAPIKeyWithNotExistingAPIKey(c *check.C) {
    34  	u, err := GetUser(1)
    35  	c.Assert(err, check.Equals, nil)
    36  
    37  	u, err = GetUserByAPIKey(u.ApiKey + "test")
    38  	c.Assert(err, check.Equals, gorm.ErrRecordNotFound)
    39  	c.Assert(u.Username, check.Equals, "")
    40  }
    41  
    42  func (s *ModelsSuite) TestGetUserByUsernameWithNotExistingUser(c *check.C) {
    43  	u, err := GetUserByUsername("test user does not exist")
    44  	c.Assert(err, check.Equals, gorm.ErrRecordNotFound)
    45  	c.Assert(u.Username, check.Equals, "")
    46  }
    47  
    48  func (s *ModelsSuite) TestPutUser(c *check.C) {
    49  	u, err := GetUser(1)
    50  	u.Username = "admin_changed"
    51  	err = PutUser(&u)
    52  	c.Assert(err, check.Equals, nil)
    53  	u, err = GetUser(1)
    54  	c.Assert(u.Username, check.Equals, "admin_changed")
    55  }
    56  
    57  func (s *ModelsSuite) TestGeneratedAPIKey(c *check.C) {
    58  	u, err := GetUser(1)
    59  	c.Assert(err, check.Equals, nil)
    60  	c.Assert(u.ApiKey, check.Not(check.Equals), "12345678901234567890123456789012")
    61  }
    62  
    63  func (s *ModelsSuite) verifyRoleCount(c *check.C, roleID, expected int64) {
    64  	var adminCount int64
    65  	err := db.Model(&User{}).Where("role_id=?", roleID).Count(&adminCount).Error
    66  	c.Assert(err, check.Equals, nil)
    67  	c.Assert(adminCount, check.Equals, expected)
    68  }
    69  
    70  func (s *ModelsSuite) TestDeleteLastAdmin(c *check.C) {
    71  	// Create a new admin user
    72  	role, err := GetRoleBySlug(RoleAdmin)
    73  	c.Assert(err, check.Equals, nil)
    74  	newAdmin := User{
    75  		Username: "new-admin",
    76  		Hash:     "123456",
    77  		ApiKey:   "123456",
    78  		Role:     role,
    79  		RoleID:   role.ID,
    80  	}
    81  	err = PutUser(&newAdmin)
    82  	c.Assert(err, check.Equals, nil)
    83  
    84  	// Ensure that there are two admins
    85  	s.verifyRoleCount(c, role.ID, 2)
    86  
    87  	// Delete the newly created admin - this should work since we have more
    88  	// than one current admin.
    89  	err = DeleteUser(newAdmin.Id)
    90  	c.Assert(err, check.Equals, nil)
    91  
    92  	// Verify that we now have one admin
    93  	s.verifyRoleCount(c, role.ID, 1)
    94  
    95  	// Try to delete the last admin - this should fail since we always want at
    96  	// least one admin active in Gophish.
    97  	err = DeleteUser(1)
    98  	c.Assert(err, check.Equals, ErrModifyingOnlyAdmin)
    99  
   100  	// Verify that the admin wasn't deleted
   101  	s.verifyRoleCount(c, role.ID, 1)
   102  }