88 "bytes"
99 "io"
1010 "io/ioutil"
11+ "strings"
1112 "testing"
1213)
1314
@@ -192,6 +193,16 @@ func TestNewMachine(t *testing.T) {
192193 loginVal := "dodging-samurai-42@heroku.com"
193194 passwordVal := "octocatdodgeballchampions"
194195 accountVal := "someacct"
196+
197+ // sanity check
198+ bodyb , _ := n .MarshalText ()
199+ body := string (bodyb )
200+ for _ , value := range []string {nameVal , loginVal , passwordVal , accountVal } {
201+ if strings .Contains (body , value ) {
202+ t .Errorf ("MarshalText() before NewMachine() contained unexpected %q" , value )
203+ }
204+ }
205+
195206 m := n .NewMachine (nameVal , loginVal , passwordVal , accountVal )
196207 if m == nil {
197208 t .Fatalf ("NewMachine() returned nil" )
@@ -214,6 +225,14 @@ func TestNewMachine(t *testing.T) {
214225 checkToken (t , "logintoken" , m .logintoken , tkLogin , "\n \t login" , loginVal )
215226 checkToken (t , "passtoken" , m .passtoken , tkPassword , "\n \t password" , passwordVal )
216227 checkToken (t , "accounttoken" , m .accounttoken , tkAccount , "\n \t account" , accountVal )
228+ // check marshal output
229+ bodyb , _ = n .MarshalText ()
230+ body = string (bodyb )
231+ for _ , value := range []string {nameVal , loginVal , passwordVal , accountVal } {
232+ if ! strings .Contains (body , value ) {
233+ t .Errorf ("MarshalText() after NewMachine() did not include %q as expected" , value )
234+ }
235+ }
217236}
218237
219238func checkToken (t * testing.T , name string , tok * token , kind tkType , rawkind , value string ) {
@@ -243,6 +262,71 @@ type tokenss struct {
243262 rawvalue []byte
244263}
245264
265+ func TestUpdateLogin (t * testing.T ) {
266+ n , err := ParseFile ("examples/good.netrc" )
267+ if err != nil {
268+ t .Fatal (err )
269+ }
270+
271+ tests := []struct {
272+ exists bool
273+ name string
274+ oldlogin string
275+ newlogin string
276+ }{
277+ {true , "mail.google.com" , "joe@gmail.com" , "joe2@gmail.com" },
278+ {false , "heroku.com" , "" , "dodging-samurai-42@heroku.com" },
279+ }
280+
281+ bodyb , _ := n .MarshalText ()
282+ body := string (bodyb )
283+ for _ , test := range tests {
284+ if strings .Contains (body , test .newlogin ) {
285+ t .Errorf ("MarshalText() before UpdateLogin() contained unexpected %q" , test .newlogin )
286+ }
287+ }
288+
289+ for _ , test := range tests {
290+ m , def , err := n .FindMachine (test .name )
291+ if err != nil {
292+ t .Fatal (err )
293+ }
294+ if def == test .exists {
295+ t .Errorf ("expected machine %s to not exist, but it did" , test .name )
296+ } else {
297+ if ! test .exists {
298+ m = n .NewMachine (test .name , test .newlogin , "" , "" )
299+ }
300+ if m == nil {
301+ t .Errorf ("machine %s was nil" , test .name )
302+ continue
303+ }
304+ m .UpdateLogin (test .newlogin )
305+ m , _ , err := n .FindMachine (test .name )
306+ if err != nil {
307+ t .Fatal (err )
308+ }
309+ if m .Login != test .newlogin {
310+ t .Errorf ("expected new login %q, got %q" , test .newlogin , m .Login )
311+ }
312+ if m .logintoken .value != test .newlogin {
313+ t .Errorf ("expected m.logintoken %q, got %q" , test .newlogin , m .logintoken .value )
314+ }
315+ }
316+ }
317+
318+ bodyb , _ = n .MarshalText ()
319+ body = string (bodyb )
320+ for _ , test := range tests {
321+ if test .exists && strings .Contains (body , test .oldlogin ) {
322+ t .Errorf ("MarshalText() after UpdateLogin() contained unexpected %q" , test .oldlogin )
323+ }
324+ if ! strings .Contains (body , test .newlogin ) {
325+ t .Errorf ("MarshalText after UpdatePassword did not contain %q as expected" , test .newlogin )
326+ }
327+ }
328+ }
329+
246330func TestUpdatePassword (t * testing.T ) {
247331 n , err := ParseFile ("examples/good.netrc" )
248332 if err != nil {
@@ -252,11 +336,22 @@ func TestUpdatePassword(t *testing.T) {
252336 tests := []struct {
253337 exists bool
254338 name string
255- newlogin string
339+ oldpassword string
256340 newpassword string
257341 }{
258- {true , "ray" , "joe2@gmail.com" , "supernewpass" },
259- {false , "heroku.com" , "dodging-samurai-42@heroku.com" , "octocatdodgeballchampions" },
342+ {true , "ray" , "mypassword" , "supernewpass" },
343+ {false , "heroku.com" , "" , "octocatdodgeballchampions" },
344+ }
345+
346+ bodyb , _ := n .MarshalText ()
347+ body := string (bodyb )
348+ for _ , test := range tests {
349+ if test .exists && ! strings .Contains (body , test .oldpassword ) {
350+ t .Errorf ("MarshalText() before UpdatePassword() did not include %q as expected" , test .oldpassword )
351+ }
352+ if strings .Contains (body , test .newpassword ) {
353+ t .Errorf ("MarshalText() before UpdatePassword() contained unexpected %q" , test .newpassword )
354+ }
260355 }
261356
262357 for _ , test := range tests {
@@ -268,7 +363,7 @@ func TestUpdatePassword(t *testing.T) {
268363 t .Errorf ("expected machine %s to not exist, but it did" , test .name )
269364 } else {
270365 if ! test .exists {
271- m = n .NewMachine (test .name , test . newlogin , test .newpassword , "" )
366+ m = n .NewMachine (test .name , "" , test .newpassword , "" )
272367 }
273368 if m == nil {
274369 t .Errorf ("machine %s was nil" , test .name )
@@ -287,6 +382,17 @@ func TestUpdatePassword(t *testing.T) {
287382 }
288383 }
289384 }
385+
386+ bodyb , _ = n .MarshalText ()
387+ body = string (bodyb )
388+ for _ , test := range tests {
389+ if test .exists && strings .Contains (body , test .oldpassword ) {
390+ t .Errorf ("MarshalText() after UpdatePassword() contained unexpected %q" , test .oldpassword )
391+ }
392+ if ! strings .Contains (body , test .newpassword ) {
393+ t .Errorf ("MarshalText() after UpdatePassword() did not contain %q as expected" , test .newpassword )
394+ }
395+ }
290396}
291397
292398func netrcReader (filename string , t * testing.T ) io.Reader {
0 commit comments