github.com/2lambda123/git-lfs@v2.5.2+incompatible/locking/locks_test.go (about)

     1  package locking
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"sort"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/git-lfs/git-lfs/lfsapi"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  type LocksById []Lock
    18  
    19  func (a LocksById) Len() int           { return len(a) }
    20  func (a LocksById) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    21  func (a LocksById) Less(i, j int) bool { return a[i].Id < a[j].Id }
    22  
    23  func TestRefreshCache(t *testing.T) {
    24  	var err error
    25  	tempDir, err := ioutil.TempDir("", "testCacheLock")
    26  	assert.Nil(t, err)
    27  
    28  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    29  		assert.Equal(t, "POST", r.Method)
    30  		assert.Equal(t, "/api/locks/verify", r.URL.Path)
    31  
    32  		w.Header().Set("Content-Type", "application/json")
    33  		err = json.NewEncoder(w).Encode(lockVerifiableList{
    34  			Theirs: []Lock{
    35  				Lock{Id: "99", Path: "folder/test3.dat", Owner: &User{Name: "Alice"}},
    36  				Lock{Id: "199", Path: "other/test1.dat", Owner: &User{Name: "Charles"}},
    37  			},
    38  			Ours: []Lock{
    39  				Lock{Id: "101", Path: "folder/test1.dat", Owner: &User{Name: "Fred"}},
    40  				Lock{Id: "102", Path: "folder/test2.dat", Owner: &User{Name: "Fred"}},
    41  				Lock{Id: "103", Path: "root.dat", Owner: &User{Name: "Fred"}},
    42  			},
    43  		})
    44  		assert.Nil(t, err)
    45  	}))
    46  
    47  	defer func() {
    48  		srv.Close()
    49  	}()
    50  
    51  	lfsclient, err := lfsapi.NewClient(lfsapi.NewContext(nil, nil, map[string]string{
    52  		"lfs.url":    srv.URL + "/api",
    53  		"user.name":  "Fred",
    54  		"user.email": "fred@bloggs.com",
    55  	}))
    56  	require.Nil(t, err)
    57  
    58  	client, err := NewClient("", lfsclient)
    59  	assert.Nil(t, err)
    60  	assert.Nil(t, client.SetupFileCache(tempDir))
    61  
    62  	// Should start with no cached items
    63  	locks, err := client.SearchLocks(nil, 0, true)
    64  	assert.Nil(t, err)
    65  	assert.Empty(t, locks)
    66  
    67  	_, _, err = client.VerifiableLocks(nil, 100)
    68  	assert.Nil(t, err)
    69  
    70  	locks, err = client.SearchLocks(nil, 0, true)
    71  	assert.Nil(t, err)
    72  	// Need to include zero time in structure for equal to work
    73  	zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
    74  
    75  	// Sort locks for stable comparison
    76  	sort.Sort(LocksById(locks))
    77  	assert.Equal(t, []Lock{
    78  		Lock{Path: "folder/test1.dat", Id: "101", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
    79  		Lock{Path: "folder/test2.dat", Id: "102", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
    80  		Lock{Path: "root.dat", Id: "103", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
    81  		Lock{Path: "other/test1.dat", Id: "199", Owner: &User{Name: "Charles"}, LockedAt: zeroTime},
    82  		Lock{Path: "folder/test3.dat", Id: "99", Owner: &User{Name: "Alice"}, LockedAt: zeroTime},
    83  	}, locks)
    84  }
    85  
    86  func TestGetVerifiableLocks(t *testing.T) {
    87  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    88  		assert.Equal(t, "POST", r.Method)
    89  		assert.Equal(t, "/api/locks/verify", r.URL.Path)
    90  
    91  		body := lockVerifiableRequest{}
    92  		if assert.Nil(t, json.NewDecoder(r.Body).Decode(&body)) {
    93  			w.Header().Set("Content-Type", "application/json")
    94  			list := lockVerifiableList{}
    95  			if body.Cursor == "1" {
    96  				list.Ours = []Lock{
    97  					Lock{Path: "folder/1/test1.dat", Id: "111"},
    98  				}
    99  				list.Theirs = []Lock{
   100  					Lock{Path: "folder/1/test2.dat", Id: "112"},
   101  					Lock{Path: "folder/1/test3.dat", Id: "113"},
   102  				}
   103  			} else {
   104  				list.Ours = []Lock{
   105  					Lock{Path: "folder/0/test1.dat", Id: "101"},
   106  					Lock{Path: "folder/0/test2.dat", Id: "102"},
   107  				}
   108  				list.Theirs = []Lock{
   109  					Lock{Path: "folder/0/test3.dat", Id: "103"},
   110  				}
   111  				list.NextCursor = "1"
   112  			}
   113  
   114  			err := json.NewEncoder(w).Encode(&list)
   115  			assert.Nil(t, err)
   116  		} else {
   117  			w.WriteHeader(500)
   118  		}
   119  	}))
   120  
   121  	defer srv.Close()
   122  
   123  	lfsclient, err := lfsapi.NewClient(lfsapi.NewContext(nil, nil, map[string]string{
   124  		"lfs.url":    srv.URL + "/api",
   125  		"user.name":  "Fred",
   126  		"user.email": "fred@bloggs.com",
   127  	}))
   128  	require.Nil(t, err)
   129  
   130  	client, err := NewClient("", lfsclient)
   131  	assert.Nil(t, err)
   132  
   133  	ourLocks, theirLocks, err := client.VerifiableLocks(nil, 0)
   134  	assert.Nil(t, err)
   135  
   136  	// Need to include zero time in structure for equal to work
   137  	zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
   138  
   139  	// Sort locks for stable comparison
   140  	sort.Sort(LocksById(ourLocks))
   141  	assert.Equal(t, []Lock{
   142  		Lock{Path: "folder/0/test1.dat", Id: "101", LockedAt: zeroTime},
   143  		Lock{Path: "folder/0/test2.dat", Id: "102", LockedAt: zeroTime},
   144  		Lock{Path: "folder/1/test1.dat", Id: "111", LockedAt: zeroTime},
   145  	}, ourLocks)
   146  
   147  	sort.Sort(LocksById(theirLocks))
   148  	assert.Equal(t, []Lock{
   149  		Lock{Path: "folder/0/test3.dat", Id: "103", LockedAt: zeroTime},
   150  		Lock{Path: "folder/1/test2.dat", Id: "112", LockedAt: zeroTime},
   151  		Lock{Path: "folder/1/test3.dat", Id: "113", LockedAt: zeroTime},
   152  	}, theirLocks)
   153  }