social-app-demo /amexsbs/railsapp/vendor/plugins/facebooker/test/facebooker/rails_integration_test.rb

Language Ruby Lines 1544
MD5 Hash 2e13bd722675080c195572248a858719 Estimated Cost $34,811 (why?)
Repository https://bitbucket.org/AcireStudios/social-app-demo.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
require File.expand_path(File.dirname(__FILE__) + '/../rails_test_helper')

module FBConnectTestHelpers
  def setup_fb_connect_cookies(params=cookie_hash_for_auth)
    params.each {|k,v| @request.cookies[ENV['FACEBOOK_API_KEY']+k] = CGI::Cookie.new(ENV['FACEBOOK_API_KEY']+k,v).first}
  end

  def expired_cookie_hash_for_auth
    {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
  end

  def cookie_hash_for_auth
    {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
  end
end
class NoisyController < ActionController::Base
  include Facebooker::Rails::Controller
  def rescue_action(e) raise e end
end


class ControllerWhichRequiresExtendedPermissions< NoisyController
  ensure_authenticated_to_facebook
  before_filter :ensure_has_status_update
  before_filter :ensure_has_photo_upload
  before_filter :ensure_has_video_upload
  before_filter :ensure_has_create_listing
  before_filter :ensure_has_create_event
  def index
    render :text => 'score!'
  end
end

class FBConnectController < NoisyController
  before_filter :create_facebook_session
  def index
    render :text => 'score!'
  end
end

class FBConnectControllerProxy < NoisyController
  before_filter :create_facebook_session_with_secret
  def index
    render :text => 'score!'
  end
end

class ControllerWhichRequiresFacebookAuthentication < NoisyController
  ensure_authenticated_to_facebook
  def index
    render :text => 'score!'
  end
  def link_test
    options = {}
    options[:canvas] = true if params[:canvas] == true
    options[:canvas] = false if params[:canvas] == false
    render :text=>url_for(options)
  end
  
  def named_route_test
    render :text=>comments_url()
  end
  
  def image_test
    render :inline=>"<%=image_tag 'image.png'%>"
  end
  
  def fb_params_test
    render :text=>facebook_params['user']
  end
  
  def publisher_test
    if wants_interface?
      render :text=>"interface"
    else
      render :text=>"not interface"
    end
  end
  
  def publisher_test_interface
    render_publisher_interface("This is a test",false,true)
  end
  
  def publisher_test_response
    ua=Facebooker::Rails::Publisher::UserAction.new
    ua.data = {:params=>true}
    ua.template_name = "template_name"
    ua.template_id =  1234
    render_publisher_response(ua)
  end
  
  def publisher_test_error
    render_publisher_error("Title","Body")
  end
  
end
class ControllerWhichRequiresApplicationInstallation < NoisyController
  ensure_application_is_installed_by_facebook_user
  def index
    render :text => 'installed!'
  end    
end
class FacebookController < ActionController::Base
  def index
  end
end

class PlainOldRailsController < ActionController::Base
  def index
  end
  def link_test
    options = {}
    options[:canvas] = true if params[:canvas] == true
    options[:canvas] = false if params[:canvas] == false
    render :text => url_for(options)
  end
  
  def named_route_test
    render :text=>comments_url()
  end
  def canvas_false_test
    render :text=>comments_url(:canvas=>false)
  end
  def canvas_true_test
    render :text=>comments_url(:canvas=>true)
  end
end


# you can't use asset_recognize, because it can't pass parameters in to the requests
class UrlRecognitionTests < Test::Unit::TestCase
  def test_detects_in_canvas
    if Rails.version < '2.3'
      request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil)
    else
      request = ActionController::TestRequest.new
      request.query_parameters[:fb_sig_in_canvas] = "1"
    end
    request.path   = "/"
    ActionController::Routing::Routes.recognize(request)
    assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters)
  end
  
  def test_routes_when_not_in_canvas
    if Rails.version < '2.3'
      request = ActionController::TestRequest.new({}, {}, nil)
    else
      request = ActionController::TestRequest.new
    end
    request.path   = "/"
    ActionController::Routing::Routes.recognize(request)
    assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters)
  end
end

class RailsIntegrationTestForFBConnect < Test::Unit::TestCase
  include FBConnectTestHelpers
  
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'facebook_app_name',
      'secret_key'       => '7654321' })
    @controller = FBConnectController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller.stubs(:verify_signature).returns(true)
            
  end
  
  def test_doesnt_set_cookie_but_facebook_session_is_available
    setup_fb_connect_cookies
    get :index
    assert_not_nil @controller.facebook_session
    assert_nil @response.cookies[:facebook_session] 
    
  end
end

class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'facebook_app_name',
      'secret_key'       => '7654321' })
    @controller = PlainOldRailsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new        
  end

  def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
    get :link_test
    assert_match(/test.host/, @response.body)
  end
  
  def test_named_route_doesnt_include_canvas_path_when_not_in_canvas
    get :named_route_test
    assert_equal "http://test.host/comments",@response.body
  end
  def test_named_route_includes_canvas_path_when_in_canvas
    get :named_route_test, facebook_params
    assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
  end
 
  def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false
    get :canvas_false_test, facebook_params
    assert_equal "http://test.host/comments",@response.body
  end
  def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true
    get :canvas_true_test
    assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
  end
  
end
  
class RailsIntegrationTestForExtendedPermissions < Test::Unit::TestCase
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'secret_key'       => '7654321' })
    @controller = ControllerWhichRequiresExtendedPermissions.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller.stubs(:verify_signature).returns(true)
  end
  
  def test_redirects_without_set_status
    post :index, facebook_params
    assert_response :success
    assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=status_update\" />", @response.body)
  end
  def test_redirects_without_photo_upload
    post :index, facebook_params(:fb_sig_ext_perms=>"status_update,create_event")
    assert_response :success
    assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=photo_upload\" />", @response.body)
  end
  def test_redirects_without_video_upload
    post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,create_event")
    assert_response :success
    assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=video_upload\" />", @response.body)
  end
  def test_redirects_without_create_listing
    post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,video_upload,create_event")
    assert_response :success
    assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=create_listing\" />", @response.body)
  end
  def test_redirects_without_create_event
    post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing,video_upload")
    assert_response :success
    assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=create_event\" />", @response.body)
  end
  
  def test_renders_with_permission
    post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing,video_upload,create_event")
    assert_response :success
    assert_equal("score!", @response.body)
    
  end
end  
  
class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'secret_key'       => '7654321' })
    @controller = ControllerWhichRequiresApplicationInstallation.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller.stubs(:verify_signature).returns(true)
  end
  
  def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page
    get :index
    assert_response :redirect
    assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0&next=http%3A%2F%2Ftest.host%2Frequire_install", @response.headers['Location'])
  end
  
  def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page
    get :index, facebook_params(:fb_sig_added => nil)
    assert_response :success
    assert(@response.body =~ /fb:redirect/)
  end
  
  def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render
    get :index, facebook_params('fb_sig_added' => "1")
    assert_response :success
    assert_equal("installed!", @response.body)
  end
end
  
class RailsIntegrationTest < Test::Unit::TestCase
  include FBConnectTestHelpers
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'root',
      'secret_key'       => '7654321',
      'set_asset_host_to_callback_url' => true,
      'callback_url'     => "http://root.example.com" })
    @controller = ControllerWhichRequiresFacebookAuthentication.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new    
    @controller.stubs(:verify_signature).returns(true)
    
  end
  
  def test_named_route_includes_new_canvas_path_when_in_new_canvas
    get :named_route_test, facebook_params("fb_sig_in_new_facebook"=>"1")
    assert_equal "http://apps.facebook.com/root/comments",@response.body
  end

  def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect
    get :index
    assert_response :redirect
    assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0&next=http%3A%2F%2Ftest.host%2Frequire_auth", @response.headers['Location'])
  end

  def test_facebook_params_are_parsed_into_a_separate_hash
    get :index, facebook_params(:fb_sig_user => '9')
    assert_not_nil @controller.facebook_params['time']
  end
  
  def test_facebook_params_convert_in_canvas_to_boolean
    get :index, facebook_params
    assert_equal(true, @controller.facebook_params['in_canvas'])    
  end
  
  def test_facebook_params_convert_added_to_boolean_false
    get :index, facebook_params(:fb_sig_added => '0')
    assert_equal(false, @controller.facebook_params['added'])
  end
  
  def test_facebook_params_convert_added_to_boolean_true
    get :index, facebook_params('fb_sig_added' => "1")
    assert_equal(true, @controller.facebook_params['added'])
  end

  def test_facebook_params_convert_added_to_boolean_false_when_already_false
    get :index, facebook_params('fb_sig_added' => false)
    assert_equal(false, @controller.facebook_params['added'])
  end

  def test_facebook_params_convert_added_to_boolean_true_when_already_true
    get :index, facebook_params('fb_sig_added' => true)
    assert_equal(true, @controller.facebook_params['added'])
  end
  
  def test_facebook_params_convert_expirey_into_nil
    get :index, facebook_params(:fb_sig_expires => '0')
    assert_nil(@controller.facebook_params['expires'])
  end
  
  def test_facebook_params_convert_expirey_into_time
    get :index, facebook_params(:fb_sig_expires => 5.minutes.from_now.to_f)
    assert_instance_of Time, @controller.facebook_params['expires']
  end
  
  def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids
    get :index, facebook_params(:fb_sig_friends => '1,2,3,4,5')
    assert_kind_of(Array, @controller.facebook_params['friends'])    
    assert_equal(5, @controller.facebook_params['friends'].size)
  end
  
  def test_session_can_be_resecured_from_facebook_params
    get :index, facebook_params(:fb_sig_user => 10)
    assert_equal(10, @controller.facebook_session.user.id)    
  end
  
  def test_existing_secured_session_is_used_if_available
    session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
    session.secure_with!("session_key", "111", Time.now.to_i + 60)
    get :index, facebook_params(:fb_sig_session_key => 'session_key', :fb_sig_user => '987'), {:facebook_session => session}
    assert_equal(111, @controller.facebook_session.user.id)
  end

  def test_facebook_params_used_if_existing_secured_session_key_does_not_match
    session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
    session.secure_with!("different session key", "111", Time.now.to_i + 60)
    get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '123'), {:facebook_session => session}
    assert_equal(123, @controller.facebook_session.user.id)
  end

  def test_existing_secured_session_is_NOT_used_if_available_and_facebook_params_session_key_is_nil_and_in_canvas
    session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
    session.secure_with!("session_key", "111", Time.now.to_i + 60)
    session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
    get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '987'), {:facebook_session => session}
    assert_equal(987, @controller.facebook_session.user.id)
  end

  def test_existing_secured_session_IS_used_if_available_and_facebook_params_session_key_is_nil_and_NOT_in_canvas
    @contoller = PlainOldRailsController.new
    session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
    session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
    get :index,{}, {:facebook_session => session}
    
    assert_equal(1111111, @controller.facebook_session.user.id)
  end

  def test_session_can_be_secured_with_secret
    @controller = FBConnectControllerProxy.new
    auth_token = 'ohaiauthtokenhere111'
    modified_params = facebook_params
    modified_params.delete('fb_sig_session_key')
    modified_params['auth_token'] = auth_token
    modified_params['generate_session_secret'] = true
    session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
    session_params = { 'session_key' => '123', 'uid' => '321' }
    session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "1").once.and_return(session_params).ordered
    flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
    get :index, modified_params
  end

  def test_session_can_be_secured_with_auth_token
    auth_token = 'ohaiauthtokenhere111'
    modified_params = facebook_params
    modified_params.delete('fb_sig_session_key')
    modified_params['auth_token'] = auth_token
    session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
    session_params = { 'session_key' => '123', 'uid' => '321' }
    session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "0").once.and_return(session_params).ordered
    flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
    get :index, modified_params
  end
  
  def test_session_secured_with_auth_token_if_cookies_expired
      auth_token = 'ohaiauthtokenhere111'
      modified_params = facebook_params
      modified_params.delete('fb_sig_session_key')
      modified_params['auth_token'] = auth_token
      session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
      session_params = { 'session_key' => '123', 'uid' => '321' }
      session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "0").once.and_return(session_params).ordered
      flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
      setup_fb_connect_cookies(expired_cookie_hash_for_auth)
      get :index, modified_params
      assert_equal(321, @controller.facebook_session.user.id)
  end
          
  def test_session_can_be_secured_with_cookies
    setup_fb_connect_cookies
    get :index
    assert_equal(77777, @controller.facebook_session.user.id)
  end
  
  def test_session_does_NOT_secure_with_expired_cookies
    setup_fb_connect_cookies(expired_cookie_hash_for_auth)
    get :index
    assert_nil(@controller.facebook_session)
  end
      
  def test_user_friends_can_be_populated_from_facebook_params_if_available
    get :index, facebook_params(:fb_sig_friends => '1,2,3,4')
    friends = @controller.facebook_session.user.friends
    assert_not_nil(friends)
    assert_equal(4, friends.size)    
  end
  
  def test_fbml_redirect_tag_handles_hash_parameters_correctly
    get :index, facebook_params
    assert_equal "<fb:redirect url=\"http://apps.facebook.com/root/require_auth\" />", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true)
  end
  
  def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas
    get :index, facebook_params(:fb_sig_user => nil)
    assert_response :success
    assert @response.body =~ /fb:redirect/
  end
  
  def test_redirect_to_renders_javascript_redirect_if_request_is_for_a_facebook_iframe
    get :index, facebook_params(:fb_sig_user => nil, :fb_sig_in_iframe => 1)
    assert_response :success
    assert_match "javascript", @response.body
    assert_match "http-equiv", @response.body
    assert_match "http://www.facebook.com/login.php?api_key=1234567&amp;v=1.0", @response.body
  end

  def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas
    get :link_test, facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
    assert_match(/apps.facebook.com/, @response.body)
  end

  def test_includes_relative_url_root_when_linked_to_canvas
    get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
    assert_match(/root/,@response.body)
  end

  def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
    get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
    assert_match(/test.host/,@response.body)
  end

  def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas
    get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
    assert !@response.body.match(/root/)
  end
  
  def test_url_for_links_to_canvas_if_fb_sig_is_ajax_is_true_and_fb_sig_in_canvas_is_not_true
    # Normal fb ajax calls have no fb_sig_canvas_param but we must explicitly set it to 0 because it is set to 1 in default_facebook_parameters in test helpers
    get :link_test, facebook_params(:fb_sig_is_ajax=>1, :fb_sig_in_canvas=>0)
    assert_match(/apps.facebook.com/, @response.body)
  end
  
  def test_default_url_omits_fb_params
    get :index,facebook_params(:fb_sig_friends=>"overwriteme",:get_param=>"yes")
    assert_equal "http://apps.facebook.com/root/require_auth?get_param=yes", @controller.send(:default_after_facebook_login_url)
  end

  def test_url_for_links_to_canvas_if_canvas_is_not_set
    get :link_test,facebook_params
    assert_match(/apps.facebook.com/,@response.body)
  end

  def test_image_tag
    get :image_test, facebook_params
    assert_equal "<img alt=\"Image\" src=\"http://root.example.com/images/image.png\" />",@response.body
  end
  
  def test_wants_interface_is_available_and_detects_method
    get :publisher_test, facebook_params(:method=>"publisher_getInterface")
    assert_equal "interface",@response.body
  end
  def test_wants_interface_is_available_and_detects_not_interface
    get :publisher_test, facebook_params(:method=>"publisher_getFeedStory")
    assert_equal "not interface",@response.body
  end
  
  def test_publisher_test_error
    get :publisher_test_error, facebook_params
    assert_equal Facebooker.json_decode("{\"errorCode\": 1, \"errorTitle\": \"Title\", \"errorMessage\": \"Body\"}"), Facebooker.json_decode(@response.body)
  end
  
  def test_publisher_test_interface
    get :publisher_test_interface, facebook_params
    assert_equal Facebooker.json_decode("{\"method\": \"publisher_getInterface\", \"content\": {\"fbml\": \"This is a test\", \"publishEnabled\": false, \"commentEnabled\": true}}"), Facebooker.json_decode(@response.body)
  end
  
  def test_publisher_test_reponse
    get :publisher_test_response, facebook_params
    assert_equal Facebooker.json_decode("{\"method\": \"publisher_getFeedStory\", \"content\": {\"feed\": {\"template_data\": {\"params\": true}, \"template_id\": 1234}}}"), Facebooker.json_decode(@response.body)
    
  end
  
  private

  def expired_cookie_hash_for_auth
    {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
  end

  def cookie_hash_for_auth
    {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
  end
   
end


class RailsSignatureTest < Test::Unit::TestCase
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'root',
      'secret_key'       => '7654321' })
    @controller = ControllerWhichRequiresFacebookAuthentication.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new    

  end

  if Rails.version < '2.3'
  
    def test_should_raise_on_bad_sig
      begin
        get :fb_params_test, facebook_params.merge('fb_sig' => 'incorrect')
        fail "No IncorrectSignature raised"
      rescue Facebooker::Session::IncorrectSignature=>e
      end
    end

    def test_valid_signature
      @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1)
      get :fb_params_test, facebook_params 
    end

  end
  
  def test_should_raise_too_old_for_replayed_session
    begin
      get :fb_params_test, facebook_params('fb_sig_time' => Time.now.to_i - 49.hours)
      fail "No SignatureTooOld raised"
    rescue Facebooker::Session::SignatureTooOld=>e
    end
  end
  
end
class RailsHelperTest < Test::Unit::TestCase
  class HelperClass
    include ActionView::Helpers::TextHelper
    include ActionView::Helpers::CaptureHelper
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::AssetTagHelper
    include ActionView::Helpers::JavaScriptHelper
    include Facebooker::Rails::Helpers
    attr_accessor :flash, :output_buffer
    def initialize
      @flash={}
      @template = self
      @content_for_test_param="Test Param"
      @output_buffer = ""
    end
    #used for stubbing out the form builder
    def url_for(arg)
      arg
    end
    def request
      ActionController::TestRequest.new
    end
    def fields_for(*args)
      ""
    end
        
  end 

  # used for capturing the contents of some of the helper tests
  # this duplicates the rails template system  
  attr_accessor :_erbout
  
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'facebook',
      'secret_key'       => '7654321' })
    @_erbout = ""
    @h = HelperClass.new
    #use an asset path where the canvas path equals the hostname to make sure we handle that case right
    ActionController::Base.asset_host='http://facebook.host.com'
  end
  
  def test_fb_profile_pic
    assert_equal "<fb:profile-pic uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234")
  end

  def test_fb_profile_pic_with_valid_size
    assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234", :size => :small)
  end

  def test_fb_profile_pic_with_width_and_height
    assert_equal "<fb:profile-pic height=\"200\" uid=\"1234\" width=\"100\"></fb:profile-pic>", @h.fb_profile_pic("1234", :width => 100, :height => 200)
  end

  def test_fb_profile_pic_with_invalid_size
    assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
  end

  def test_fb_photo
    assert_equal "<fb:photo pid=\"1234\"></fb:photo>",@h.fb_photo("1234")
  end

  def test_fb_photo_with_object_responding_to_photo_id
    photo = flexmock("photo", :photo_id => "5678")
    assert_equal "<fb:photo pid=\"5678\"></fb:photo>", @h.fb_photo(photo)
  end

  def test_fb_photo_with_invalid_size
    assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
  end
  
  def test_fb_photo_with_invalid_size_value
    assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
  end
  
  def test_fb_photo_with_invalid_align_value
    assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
  end

  def test_fb_photo_with_valid_align_value
    assert_equal "<fb:photo align=\"right\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :align => :right)
  end

  def test_fb_photo_with_class
    assert_equal "<fb:photo class=\"picky\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :class => :picky)
  end
  def test_fb_photo_with_style
    assert_equal "<fb:photo pid=\"1234\" style=\"some=css;put=here;\"></fb:photo>",@h.fb_photo("1234", :style => "some=css;put=here;")
  end
  
  def test_fb_prompt_permission_valid_no_callback
    assert_equal "<fb:prompt-permission perms=\"email\">Can I email you?</fb:prompt-permission>",@h.fb_prompt_permission("email","Can I email you?")    
  end
  
  def test_fb_prompt_permission_valid_with_callback
    assert_equal "<fb:prompt-permission next_fbjs=\"do_stuff()\" perms=\"email\">a message</fb:prompt-permission>",@h.fb_prompt_permission("email","a message","do_stuff()")
  end
  
  def test_fb_prompt_permission_invalid_option
    assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")}
    
  end
  
  def test_fb_prompt_permissions_valid_no_callback
    assert_equal "<fb:prompt-permission perms=\"publish_stream,read_stream\">Can I read and write your streams?</fb:prompt-permission>",
                 @h.fb_prompt_permissions(['publish_stream', 'read_stream'],"Can I read and write your streams?")    
  end
  
  def test_fb_prompt_permissions_valid_with_callback
    assert_equal "<fb:prompt-permission next_fbjs=\"do_stuff()\" perms=\"publish_stream,read_stream\">Can I read and write your streams?</fb:prompt-permission>",
                 @h.fb_prompt_permissions(['publish_stream', 'read_stream'],"Can I read and write your streams?", "do_stuff()")    
  end
  
  def test_fb_prompt_permissions_invalid_option
    assert_raises(ArgumentError) {@h.fb_prompt_permissions(["invliad", "read_stream"], "a message")}
    
  end  
 
  
  def test_fb_add_profile_section
    assert_equal "<fb:add-section-button section=\"profile\" />",@h.fb_add_profile_section
  end

  def test_fb_add_info_section
    assert_equal "<fb:add-section-button section=\"info\" />",@h.fb_add_info_section
  end

  def test_fb_application_name
    assert_equal "<fb:application-name />", @h.fb_application_name
  end

  def test_fb_application_name_with_linked_false
    assert_equal '<fb:application-name linked="false" />', @h.fb_application_name( :linked => false )
  end

  def test_fb_name_with_invalid_key_size
    assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
  end

  def test_fb_name
    assert_equal "<fb:name uid=\"1234\"></fb:name>",@h.fb_name("1234")
  end

  def test_fb_name_with_transformed_key
    assert_equal "<fb:name uid=\"1234\" useyou=\"true\"></fb:name>", @h.fb_name(1234, :use_you => true)
  end

  def test_fb_name_with_user_responding_to_facebook_id
    user = flexmock("user", :facebook_id => "5678")
    assert_equal "<fb:name uid=\"5678\"></fb:name>", @h.fb_name(user)
  end

  def test_fb_name_with_invalid_key_linkd
    assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
  end

  def test_fb_tabs
    assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
  end

  def test_fb_tab_item
    assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
  end

  def test_fb_tab_item_raises_exception_for_invalid_option
    assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
  end

  def test_fb_tab_item_raises_exception_for_invalid_align_value
    assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
  end
    
  def test_fb_req_choice
    assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
  end
  
  def test_fb_multi_friend_selector
    assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message")
  end
  def test_fb_multi_friend_selector_with_options
    assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" exclude_ids=\"1,2\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message",:exclude_ids=>"1,2")
  end

  def test_fb_title
    assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
  end
  
  def test_fb_google_analytics
    assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
  end

  def test_fb_if_is_user_with_single_object
    user = flexmock("user", :facebook_id => "5678")
    assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}    
  end
  
  def test_fb_if_is_user_with_array
    user1 = flexmock("user", :facebook_id => "5678")
    user2 = flexmock("user", :facebook_id => "1234")
    assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
  end
  
  def test_fb_else
    assert_equal "<fb:else></fb:else>", @h.fb_else{}    
  end
  
  def test_fb_about_url
    ENV["FACEBOOK_API_KEY"]="1234"
    assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
  end
  
  def test_fb_ref_with_url
    assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
  end
  
  def test_fb_ref_with_handle
    assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
  end
  
  def test_fb_ref_with_invalid_attribute
    assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
  end
  
  def test_fb_ref_with_handle_and_url
    assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
  end  
  
  def test_facebook_messages_notice
    @h.flash[:notice]="A message"
    assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
  end
  
  def test_facebook_messages_error
    @h.flash[:error]="An error"
    assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
  end
  def test_fb_wall_post
    assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
  end
  
  def test_fb_pronoun
    assert_equal "<fb:pronoun uid=\"1234\"></fb:pronoun>", @h.fb_pronoun(1234)
  end
  
  def test_fb_pronoun_with_transformed_key
    assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\"></fb:pronoun>", @h.fb_pronoun(1234, :use_they => true)
  end
  
  def test_fb_pronoun_with_user_responding_to_facebook_id
    user = flexmock("user", :facebook_id => "5678")
    assert_equal "<fb:pronoun uid=\"5678\"></fb:pronoun>", @h.fb_pronoun(user)
  end
  
  def test_fb_pronoun_with_invalid_key
    assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
  end
  
  def test_fb_wall
    @h.expects(:capture).returns("wall content")
    @h.fb_wall do 
    end
    assert_equal "<fb:wall>wall content</fb:wall>",@h.output_buffer
  end
  
  def test_fb_multi_friend_request
    @h.expects(:capture).returns("body")
    @h.expects(:protect_against_forgery?).returns(false)
    @h.expects(:fb_multi_friend_selector).returns("friend selector")
    assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
      (@h.fb_multi_friend_request("invite","ignored","action") {})
  end
  
  def test_fb_multi_friend_request_with_protection_against_forgery
    @h.expects(:capture).returns("body")
    @h.expects(:protect_against_forgery?).returns(true)
    @h.expects(:request_forgery_protection_token).returns('forgery_token')
    @h.expects(:form_authenticity_token).returns('form_token')

    @h.expects(:fb_multi_friend_selector).returns("friend selector")
    assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector<input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:request-form>",
      (@h.fb_multi_friend_request("invite","ignored","action") {})
  end
  
  def test_fbjs_library
    @h.expects(:form_authenticity_token).returns('form_token')
    assert_equal "<script>var _token = 'form_token';var _hostname = 'http://facebook.host.com'</script><script src=\"http://facebook.host.com/javascripts/facebooker.js\" type=\"text/javascript\"></script>", @h.fbjs_library
  end
  
  def test_fb_dialog
    @h.expects(:capture).returns("dialog content")
    @h.fb_dialog( "my_dialog", true ) {}
    assert_equal '<fb:dialog cancel_button="1" id="my_dialog">dialog content</fb:dialog>', @h.output_buffer
  end
  def test_fb_dialog_title
    assert_equal '<fb:dialog-title>My Little Dialog</fb:dialog-title>', @h.fb_dialog_title("My Little Dialog")
  end
  def test_fb_dialog_content
    @h.expects(:capture).returns("dialog content content")
    @h.fb_dialog_content do
    end
    assert_equal '<fb:dialog-content>dialog content content</fb:dialog-content>', @h.output_buffer
  end
  def test_fb_dialog_button
    assert_equal '<fb:dialog-button clickrewriteform="my_form" clickrewriteid="my_dialog" clickrewriteurl="http://www.some_url_here.com/dialog_return.php" type="submit" value="Yes" />',
      @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php",
                                            :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } )
  end
  
  def test_fb_request_form
    @h.expects(:capture).returns("body")
    @h.expects(:protect_against_forgery?).returns(false)
    assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
      (@h.fb_request_form("invite","test_param","action") {})
  end

  def test_fb_request_form_with_protect_against_forgery
    @h.expects(:capture).returns("body")
    @h.expects(:protect_against_forgery?).returns(true)
    @h.expects(:request_forgery_protection_token).returns('forgery_token')
    @h.expects(:form_authenticity_token).returns('form_token')
    assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body<input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:request-form>",
      (@h.fb_request_form("invite","test_param","action") {})
  end
  
  def test_fb_error_with_only_message
    assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
  end

  def test_fb_error_with_message_and_text
    assert_equal "<fb:error><fb:message>Errors have occurred!!</fb:message>Label can't be blank!!</fb:error>", @h.fb_error("Errors have occurred!!", "Label can't be blank!!")
  end

  def test_fb_explanation_with_only_message
    assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
  end

  def test_fb_explanation_with_message_and_text
    assert_equal "<fb:explanation><fb:message>This is an explanation</fb:message>You have a match</fb:explanation>", @h.fb_explanation("This is an explanation", "You have a match")
  end

  def test_fb_success_with_only_message
    assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
  end

  def test_fb_success_with_message_and_text
    assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
  end
  
  def test_facebook_form_for
    @h.expects(:protect_against_forgery?).returns(false)
    form_body=@h.facebook_form_for(:model,:url=>"action") do
    end
    assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
  end
  
  def test_facebook_form_for_with_authenticity_token
    @h.expects(:protect_against_forgery?).returns(true)
    @h.expects(:request_forgery_protection_token).returns('forgery_token')
    @h.expects(:form_authenticity_token).returns('form_token')
    assert_equal "<fb:editor action=\"action\"><input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:editor>",
      (@h.facebook_form_for(:model, :url => "action") {})
  end
  
  def test_fb_friend_selector
    assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
  end
  
  def test_fb_request_form_submit
    assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)  
  end   

  def test_fb_request_form_submit_with_uid
    assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
  end

  def test_fb_request_form_submit_with_label
    assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
  end

  def test_fb_request_form_submit_with_uid_and_label
    assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" uid=\"123456789\" />",@h.fb_request_form_submit({:uid =>"123456789", :label => "Send Invite to Joel"}))
  end
  
  def test_fb_action
    assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")  
  end
  
  def test_fb_help
    assert_equal "<fb:help href=\"http://www.facebook.com/apps/application.php?id=6236036681\">Help</fb:help>", @h.fb_help("Help", "http://www.facebook.com/apps/application.php?id=6236036681")      
  end
  
  def test_fb_create_button
    assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
  end

  def test_fb_comments_a_1
    assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"7\" showform=\"true\" xid=\"a:1\"></fb:comments>", @h.fb_comments("a:1",true,false,7,:showform=>true)
  end

  def test_fb_comments_xxx
    assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"4\" optional=\"false\" xid=\"xxx\"></fb:comments>", @h.fb_comments("xxx",true,false,4,:optional=>false)
  end

  def test_fb_comments_with_title
    assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"4\" optional=\"false\" xid=\"xxx\"><fb:title>TITLE</fb:title></fb:comments>", @h.fb_comments("xxx",true,false,4,:optional=>false, :title => "TITLE") 
  end
  def test_fb_board
    assert_equal "<fb:board optional=\"false\" xid=\"xxx\"></fb:board>", @h.fb_board("xxx",:optional => false) 
  end
  def test_fb_board_with_title
    assert_equal "<fb:board optional=\"false\" xid=\"xxx\"><fb:title>TITLE</fb:title></fb:board>", @h.fb_board("xxx",:optional=>false, :title => "TITLE") 
  end
  
  def test_fb_dashboard
    @h.expects(:capture).returns("dashboard content")
    @h.fb_dashboard do 
    end
    assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", @h.output_buffer
  end
  def test_fb_dashboard_non_block
    assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
  end
  
  def test_fb_wide
    @h.expects(:capture).returns("wide profile content")
    @h.fb_wide do
    end
    assert_equal "<fb:wide>wide profile content</fb:wide>", @h.output_buffer
  end
  
  def test_fb_narrow
    @h.expects(:capture).returns("narrow profile content")
    @h.fb_narrow do
    end
    assert_equal "<fb:narrow>narrow profile content</fb:narrow>", @h.output_buffer
  end  
  
  def test_fb_login_button
    assert_equal "<fb:login-button onlogin=\"somejs\"></fb:login-button>",@h.fb_login_button("somejs")

    assert_equal "<fb:login-button onlogin=\"somejs\">Custom</fb:login-button>",@h.fb_login_button("somejs", :text => 'Custom')
  end
  
  def test_init_fb_connect_no_features
    assert ! @h.init_fb_connect.match(/XFBML/)
  end
  
  def test_init_fb_connect_with_features
    assert @h.init_fb_connect("XFBML").match(/XFBML/)
  end

  def test_init_fb_connect_receiver_path
    assert @h.init_fb_connect.match(/xd_receiver.html/)
  end

  def test_init_fb_connect_receiver_path_ssl
    @h.instance_eval do
      def request
        ssl_request = ActionController::TestRequest.new
        ssl_request.stubs(:ssl?).returns(true)
        ssl_request
      end
    end

    assert @h.init_fb_connect.match(/xd_receiver_ssl.html/)
  end

  def test_init_fb_connect_with_features_and_body
    @h.expects(:capture).returns("Body Content")
    
    __in_erb_template = true

    @h.init_fb_connect("XFBML") do
    end
    assert @h.output_buffer =~ /Body Content/
  end

  def test_init_fb_connect_no_options
    assert ! @h.init_fb_connect.match(/Element.observe\(window,'load',/)
  end
  
  def test_init_fb_connect_with_options_js_jquery
    assert ! @h.init_fb_connect(:js => :jquery).match(/\$\(document\).ready\(/)
  end
  
  def test_init_fb_connect_with_options_js_mootools
    assert @h.init_fb_connect("XFBML", :js => :mootools).match(/window.addEvent\('domready',/)
  end
  
  def test_init_fb_connect_with_features_and_options_js_jquery
    assert @h.init_fb_connect("XFBML", :js => :jquery).match(/XFBML.*/)
    assert @h.init_fb_connect("XFBML", :js => :jquery).match(/\jQuery\(document\).ready\(/)
  end

  def test_init_fb_connect_without_options_app_settings
    assert @h.init_fb_connect().match(/, \{\}\)/)
  end
  
  def test_init_fb_connect_with_options_app_settings
    assert @h.init_fb_connect(:app_settings => "{foo: bar}").match(/, \{foo: bar\}\)/)
  end
  
  
  def test_fb_login_and_redirect
    assert_equal @h.fb_login_and_redirect("/path"),"<fb:login-button onlogin=\"window.location.href = &quot;/path&quot;;\"></fb:login-button>"
    
    assert_equal @h.fb_login_and_redirect("/path", :text => 'foo'),"<fb:login-button onlogin=\"window.location.href = &quot;/path&quot;;\">foo</fb:login-button>"
  end
  
  def test_fb_logout_link
    assert_equal @h.fb_logout_link("Logout","My URL"),"<a href=\"#\" onclick=\"FB.Connect.logoutAndRedirect(&quot;My URL&quot;);\nwindow.location.href = &quot;My URL&quot;;; return false;\">Logout</a>"
  end

  def test_fb_bookmark_link
    assert_equal @h.fb_bookmark_link("Bookmark","My URL"),"<a href=\"#\" onclick=\"FB.Connect.showBookmarkDialog(&quot;My URL&quot;);; return false;\">Bookmark</a>"
  end

  def test_fb_user_action_with_literal_callback
    action = Facebooker::Rails::Publisher::UserAction.new
    assert_equal "FB.Connect.showFeedDialog(null, null, null, null, null, FB.RequireConnect.promptConnect, function() {alert('hi')}, \"prompt\", #{{"value" => "message"}.to_json});",
                 @h.fb_user_action(action,"message","prompt","function() {alert('hi')}")
  end

  def test_fb_user_action_with_nil_callback
    action = Facebooker::Rails::Publisher::UserAction.new
    assert_equal "FB.Connect.showFeedDialog(null, null, null, null, null, FB.RequireConnect.promptConnect, null, \"prompt\", #{{"value" => "message"}.to_json});",
                 @h.fb_user_action(action,"message","prompt")
  end
  
  def test_fb_connect_stream_publish
    stream_post = Facebooker::StreamPost.new
    attachment = Facebooker::Attachment.new
    attachment.name="name"
    stream_post.message = "message"
    stream_post.target="12451752"
    stream_post.attachment = attachment
    
    assert @h.fb_connect_stream_publish(stream_post).match(/FB.Connect\.streamPublish\(\"message\", \{\"name\":\s?\"name\"\}, \[\], \"12451752\", null, null, false, null\);/)
  end
  def test_fb_stream_publish
    stream_post = Facebooker::StreamPost.new
    attachment = Facebooker::Attachment.new
    attachment.name="name"
    stream_post.message = "message"
    stream_post.target="12451752"
    stream_post.attachment = attachment
    
    assert @h.fb_stream_publish(stream_post).match(/Facebook\.streamPublish\(\"message\", \{\"name\":\s?\"name\"\}, \[\], \"12451752\", null, null, false, null\);/)
  end

  def test_fb_connect_javascript_tag
    silence_warnings do
      assert_equal "<script src=\"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php\" type=\"text/javascript\"></script>",
        @h.fb_connect_javascript_tag
    end
  end

  def test_fb_connect_javascript_tag_with_language_option
    silence_warnings do
      assert_equal "<script src=\"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US\" type=\"text/javascript\"></script>",
        @h.fb_connect_javascript_tag(:lang => "en_US")
    end
  end

  def test_fb_connect_javascript_tag_ssl
    @h.instance_eval do
      def request
        ssl_request = ActionController::TestRequest.new
        ssl_request.stubs(:ssl?).returns(true)
        ssl_request
      end
    end

    silence_warnings do
      assert_equal "<script src=\"https://ssl.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php\" type=\"text/javascript\"></script>",
        @h.fb_connect_javascript_tag
    end
  end

  def test_fb_connect_javascript_tag_ssl_with_language_option
    @h.instance_eval do
      def request
        ssl_request = ActionController::TestRequest.new
        ssl_request.stubs(:ssl?).returns(true)
        ssl_request
      end
    end

    silence_warnings do
      assert_equal "<script src=\"https://ssl.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US\" type=\"text/javascript\"></script>",
        @h.fb_connect_javascript_tag(:lang => "en_US")
    end
  end

  def test_fb_container
    @h.expects(:capture).returns("Inner Stuff")
    @h.fb_container(:condition=>"somejs") do
    end
    assert_equal "<fb:container condition=\"somejs\">Inner Stuff</fb:container>",@h.output_buffer
  end
  
  def test_fb_eventlink
    assert_equal '<fb:eventlink eid="21150032416"></fb:eventlink>',@h.fb_eventlink("21150032416")
  end
  
  def test_fb_grouplink
    assert_equal '<fb:grouplink gid="2541896821"></fb:grouplink>',@h.fb_grouplink("2541896821")
  end
  
  def test_fb_serverfbml
    @h.expects(:capture).returns("Inner Stuff")
    @h.fb_serverfbml(:condition=>"somejs") do
    end
    assert_equal "<fb:serverfbml condition=\"somejs\">Inner Stuff</fb:serverfbml>",@h.output_buffer
  end
  
  def test_fb_share_button
    assert_equal "<fb:share-button class=\"url\" href=\"http://www.elevatedrails.com\"></fb:share-button>",@h.fb_share_button("http://www.elevatedrails.com")
  end
  
  def test_fb_unconnected_friends_count_without_condition
    assert_equal "<fb:unconnected-friends-count></fb:unconnected-friends-count>",@h.fb_unconnected_friends_count
  end
  
  def test_fb_user_status
    user=flexmock("user", :facebook_id => "5678")
    assert_equal '<fb:user-status linked="false" uid="5678"></fb:user-status>',@h.fb_user_status(user,false)
  end
  
  def test_fb_time
    time = Time.now
    assert_equal %Q{<fb:time preposition="true" t="#{time.to_i}" tz="America/New York" />}, @h.fb_time(time, :tz => 'America/New York', :preposition => true)
  end
  
  def test_fb_time_defaults
    time = Time.now
    assert_equal %Q{<fb:time t="#{time.to_i}" />}, @h.fb_time(time)
  end
end
class TestModel
  attr_accessor :name,:facebook_id
end

class RailsFacebookFormbuilderTest < Test::Unit::TestCase
  class TestTemplate
    include ActionView::Helpers::TextHelper
    include ActionView::Helpers::CaptureHelper
    include ActionView::Helpers::TagHelper
    include Facebooker::Rails::Helpers
    attr_accessor :output_buffer
    def initialize
      @output_buffer=""
    end
  end
  def setup
    @_erbout = ""
    @test_model = TestModel.new
    @test_model.name="Mike"
    @template = TestTemplate.new
    @proc = Proc.new {}
    @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
    def @form_builder._erbout
      ""
    end
    
  end
  
  def test_text_field
    assert_equal "<fb:editor-text id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\" value=\"Mike\"></fb:editor-text>",
        @form_builder.text_field(:name)
  end
  def test_text_area
    assert_equal "<fb:editor-textarea id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\">Mike</fb:editor-textarea>",
        @form_builder.text_area(:name)    
  end
  
  def test_default_name_and_id
    assert_equal "<fb:editor-text id=\"different_id\" label=\"Name\" name=\"different_name\" value=\"Mike\"></fb:editor-text>",
        @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'})
  end
  
  def test_collection_typeahead
    flexmock(@form_builder) do |fb|
      fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
    end
    @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)        
  end
  
  def test_collection_typeahead_internal
    assert_equal "<fb:typeahead-input id=\"test_model_name\" name=\"test_model[name]\" value=\"Mike\"><fb:typeahead-option value=\"3\">ABC</fb:typeahead-option></fb:typeahead-input>",
      @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)        
  end
  
  def test_buttons
    @form_builder.expects(:create_button).with(:first).returns("first")
    @form_builder.expects(:create_button).with(:second).returns("second")
    @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
    @form_builder.buttons(:first,:second)    
  end
  
  def test_create_button
    assert_equal "<fb:editor-button name=\"commit\" value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
  end
  
  def test_custom
    @template.expects(:password_field).returns("password_field")
    assert_equal "<fb:editor-custom label=\"Name\">password_field</fb:editor-custom>",@form_builder.password_field(:name)
  end
  
  def test_text
    assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
  end
  
  def test_multi_friend_input
    assert_equal "<fb:editor-custom label=\"Friends\"><fb:multi-friend-input></fb:multi-friend-input></fb:editor-custom>",@form_builder.multi_friend_input
  end
  

end

class RailsPrettyErrorsTest < Test::Unit::TestCase
  class ControllerWhichFails < ActionController::Base
    def pass
      render :text=>''
    end
    def fail
      raise "I'm failing"
    end
  end
  
  def setup
    Facebooker.apply_configuration('api_key'=>"1234", 'secret_key'=>"34278",'canvas_page_name'=>'mike','pretty_errors'=>true)
    @controller = ControllerWhichFails.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end
  
  def test_pretty_errors_disabled_success
    post :pass, facebook_params
    assert_response 200
  end

  def test_pretty_errors_disabled_error
    Facebooker.apply_configuration('api_key'=>"1234", 'secret_key'=>"34278",'canvas_page_name'=>'mike','pretty_errors'=>false)
    silence_warnings do
      post :fail, facebook_params
    end
    assert_response :error
  end

  def test_pretty_errors_enabled_success
    post :pass, facebook_params
    assert_response 200
  end
  def test_pretty_errors_enabled_error
    silence_warnings do
      post :fail, facebook_params
    end
    assert_response 200
  end
end

class RailsUrlHelperExtensionsTest < Test::Unit::TestCase
  class UrlHelperExtensionsClass
    include ActionView::Helpers::UrlHelper
    include ActionView::Helpers::TagHelper
    def initialize(controller)
      @controller = controller
    end

    def protect_against_forgery?
       false
    end
    
    def request_comes_from_facebook?
      @request_comes_from_facebook
    end
    
    def request_comes_from_facebook=(val)
      @request_comes_from_facebook = val
    end

  end 
  class UrlHelperExtensionsController < NoisyController    
    def index
      render :nothing => true
    end
    def do_it
      render :nothing => true
    end
  end

  class FacebookRequest < ActionController::TestRequest  
  end

  def setup
    @controller = UrlHelperExtensionsController.new
    @request    = FacebookRequest.new
    @response   = ActionController::TestResponse.new

    @u = UrlHelperExtensionsClass.new(@controller)
    @u.request_comes_from_facebook = true
    
    @non_canvas_u = UrlHelperExtensionsClass.new(@controller)
    @non_canvas_u.request_comes_from_facebook = false
    
    @label = "Testing"
    @url = "test.host"
    @prompt = "Are you sure?"
    @default_title = "Please Confirm"
    @title = "Confirm Request"
    @style = {:color => 'black'}
    @verbose_style = "{color: 'black'}"
    @default_okay = "Okay"
    @default_cancel = "Cancel"
    @default_style = "" #"'width','200px'"
  end

  def test_link_to
    assert_equal "<a href=\"#{@url}\">Testing</a>", @u.link_to(@label, @url)
  end

  def test_link_to_with_popup
    assert_raises(ActionView::ActionViewError) {@u.link_to(@label,@url, :popup=>true)}
  end

  def test_link_to_with_confirm
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
                 "var a=this;dlg.onconfirm = function() { " + 
                 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
                  @u.link_to(@label, @url, :confirm => @prompt) )
  end
  def test_link_to_with_confirm_with_title
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
                 "var a=this;dlg.onconfirm = function() { " + 
                 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
                  @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}) )
  end
  def test_link_to_with_confirm_with_title_and_style
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@verbose_style});"+
                 "var a=this;dlg.onconfirm = function() { " + 
                 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
                  @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}.merge!(@style)) )
  end

  def test_link_to_with_method
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var a=this;var f = document.createElement('form'); f.setStyle('display','none'); "+
                 "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
                 "var m = document.createElement('input'); m.setType('hidden'); "+
                 "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
                 "f.submit();return false;\">#{@label}</a>", @u.link_to(@label,@url, :method=>:delete))
  end

  def test_link_to_with_confirm_and_method
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
                 "var a=this;dlg.onconfirm = function() { " + 
                 "var f = document.createElement('form'); f.setStyle('display','none'); "+
                 "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
                 "var m = document.createElement('input'); m.setType('hidden'); "+
                 "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
                 "f.submit(); };return false;\">#{@label}</a>", @u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
  end
  def test_link_to_with_confirm_and_method_for_non_canvas_page
    assert_dom_equal( "<a href=\"#{@url}\" onclick=\"if (confirm(\'#{@prompt}\')) { var f = document.createElement('form'); f.style.display = 'none'; "+
                      "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); "+
                      "m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); "+
                      "f.appendChild(m);f.submit(); };return false;\">#{@label}</a>",
                      @non_canvas_u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
  end

  def test_button_to
    assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
                 "<input type=\"submit\" value=\"#{@label}\" /></div></form>", @u.button_to(@label,@url)
  end

  def test_button_to_with_confirm
    assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
                 "<input onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
                 "var a=this;dlg.onconfirm = function() { "+
                 "a.getForm().submit(); };return false;\" type=\"submit\" value=\"#{@label}\" /></div></form>", 
                 @u.button_to(@label,@url, :confirm=>@prompt)
  end
  def test_button_to_with_confirm_for_non_canvas_page
    assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>"+
                 "<input onclick=\"return confirm(\'#{@prompt}\');\" type=\"submit\" value=\"#{@label}\" /></div></form>",
                 @non_canvas_u.button_to(@label,@url, :confirm=>@prompt)
  end

  def test_link_to_unless_with_true
       assert_equal @label, @u.link_to_unless(true,@label,@url)
  end
  def test_link_to_unless_with_false
       assert_equal @u.link_to(@label,@url), @u.link_to_unless(false,@label,@url)
  end

  def test_link_to_if_with_true
       assert_equal @u.link_to(@label,@url), @u.link_to_if(true,@label,@url)
  end
  def test_link_to_if_with_false
       assert_equal @label, @u.link_to_if(false,@label,@url)
  end
  
end

class RailsRequestFormatTest < Test::Unit::TestCase
  class FacebookController < NoisyController
    def index
      respond_to do |format|
        format.html { render :text => 'html' }
        format.fbml { render :text => 'fbml' }
        format.fbjs { render :text => 'fbjs' }
      end
    end
  end
  
  def setup
    Facebooker.apply_configuration({
      'api_key'          => '1234567',
      'canvas_page_name' => 'facebook_app_name',
      'secret_key'       => '7654321' })
    @controller = FacebookController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller.stubs(:verify_signature).returns(true)
  end
  
  def test_responds_to_html_without_canvas
    get :index
    assert_equal 'html', @response.body
  end

  def test_responds_to_fbml_in_canvas
    get :index, facebook_params(:fb_sig_in_canvas => '1')
    assert_equal 'fbml', @response.body
  end

  def test_responds_to_fbjs_when_is_ajax
    get :index, facebook_params(:fb_sig_is_ajax => '1')
    assert_equal 'fbjs', @response.body
  end
  
  def test_responds_to_html_when_iframe
    get :index, facebook_params(:fb_sig_in_iframe => '1')
    assert_equal 'html', @response.body
  end
  
end
Back to Top