github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/testing/members_test.go (about)

     1  package database
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/drone/drone/pkg/database"
     7  	"github.com/drone/drone/pkg/model"
     8  )
     9  
    10  // TODO unit test to verify unique constraint on Team.Name
    11  
    12  // TestGetMember tests the ability to retrieve a Team
    13  // Member from the database by Unique ID.
    14  func TestGetMember(t *testing.T) {
    15  	Setup()
    16  	defer Teardown()
    17  
    18  	// get member by user_id and team_id
    19  	member, err := database.GetMember(1, 1)
    20  	if err != nil {
    21  		t.Error(err)
    22  	}
    23  
    24  	if member.ID != 1 {
    25  		t.Errorf("Exepected ID %d, got %d", 1, member.ID)
    26  	}
    27  
    28  	if member.Name != "Brad Rydzewski" {
    29  		t.Errorf("Exepected Name %s, got %s", "Brad Rydzewski", member.Name)
    30  	}
    31  
    32  	if member.Email != "brad.rydzewski@gmail.com" {
    33  		t.Errorf("Exepected Email %s, got %s", "brad.rydzewski@gmail.com", member.Email)
    34  	}
    35  
    36  	if member.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
    37  		t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", member.Gravatar)
    38  	}
    39  
    40  	if member.Role != model.RoleOwner {
    41  		t.Errorf("Exepected Role %s, got %s", model.RoleOwner, member.Role)
    42  	}
    43  }
    44  
    45  func TestIsMember(t *testing.T) {
    46  	Setup()
    47  	defer Teardown()
    48  
    49  	ok, err := database.IsMember(1, 1)
    50  	if err != nil {
    51  		t.Error(err)
    52  	}
    53  
    54  	if !ok {
    55  		t.Errorf("Expected IsMember to return true, returned false")
    56  	}
    57  }
    58  
    59  func TestIsMemberAdmin(t *testing.T) {
    60  	Setup()
    61  	defer Teardown()
    62  
    63  	// expecting user is Owner
    64  	if ok, err := database.IsMemberAdmin(1, 1); err != nil {
    65  		t.Error(err)
    66  	} else if !ok {
    67  		t.Errorf("Expected user id 1 IsMemberAdmin to return true, returned false")
    68  	}
    69  
    70  	// expecting user is Admin
    71  	if ok, err := database.IsMemberAdmin(2, 1); err != nil {
    72  		t.Error(err)
    73  	} else if !ok {
    74  		t.Errorf("Expected user id 2 IsMemberAdmin to return true, returned false")
    75  	}
    76  
    77  	// expecting user is NOT Admin (Write role)
    78  	if ok, err := database.IsMemberAdmin(3, 1); err != nil {
    79  		t.Error(err)
    80  	} else if ok {
    81  		t.Errorf("Expected user id 3 IsMemberAdmin to return false, returned true")
    82  	}
    83  }
    84  
    85  func TestDeleteMember(t *testing.T) {
    86  	Setup()
    87  	defer Teardown()
    88  
    89  	// delete member by user_id and team_id
    90  	if err := database.DeleteMember(1, 1); err != nil {
    91  		t.Error(err)
    92  	}
    93  
    94  	// get member by user_id and team_id
    95  	if _, err := database.GetMember(1, 1); err == nil {
    96  		t.Error(err)
    97  	}
    98  
    99  }
   100  
   101  func TestListMembers(t *testing.T) {
   102  	Setup()
   103  	defer Teardown()
   104  
   105  	// list members by team_id
   106  	members, err := database.ListMembers(1)
   107  	if err != nil {
   108  		t.Error(err)
   109  	}
   110  
   111  	// verify team count
   112  	if len(members) != 3 {
   113  		t.Errorf("Exepected %d Team Members in database, got %d", 3, len(members))
   114  		return
   115  	}
   116  
   117  	// get the first member in the list and verify
   118  	// fields are being populated correctly
   119  	member := members[0]
   120  
   121  	if member.ID != 1 {
   122  		t.Errorf("Exepected ID %d, got %d", 1, member.ID)
   123  	}
   124  
   125  	if member.Name != "Brad Rydzewski" {
   126  		t.Errorf("Exepected Name %s, got %s", "Brad Rydzewski", member.Name)
   127  	}
   128  
   129  	if member.Email != "brad.rydzewski@gmail.com" {
   130  		t.Errorf("Exepected Email %s, got %s", "brad.rydzewski@gmail.com", member.Email)
   131  	}
   132  
   133  	if member.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
   134  		t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", member.Gravatar)
   135  	}
   136  
   137  	if member.Role != model.RoleOwner {
   138  		t.Errorf("Exepected Role %s, got %s", model.RoleOwner, member.Role)
   139  	}
   140  }